I created a separate project and activity that downloads an image from a URL, converts it to a Bitmap, and then uses a FileOutputStream to save that file to the SD card. When in the separate project and free-standing activity, this worked fine, and I could see that the image is stored on the SD card.
public class PictureDownload extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downloadFromUrl("http://www.coutblog.com/prism/uploads/0030.jpg", "newpic.jpg");
displayPic("newpic.jpg");
}
// Place to download the file
private final String PATH = "/sdcard/";
public void displayPic(String filename) {
ImageView image = (ImageView)findViewById(R.id.image);
Bitmap bMap = BitmapFactory.decodeFile(PATH + filename);
image.setImageBitmap(bMap);
}
/**
* Downloads the picture from the URL to the SD card
* @param imageURL: the URL to download it from (absolute web URL)
* @param fileName: the name of the file you want to save the picture to
*/
public void downloadFromUrl(String imageURL, String fileName) {
try {
// Connect to the URL
URL myImageURL = new URL(imageURL);
HttpURLConnection connection = (HttpURLConnection)myImageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
// Get the bitmap
Bitmap myBitmap = BitmapFactory.decodeStream(input);
// Save the bitmap to the file
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, fileName);
fOut = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
} catch (IOException e) {}
}
}
But, whenever I tried to port the code to an existing project, it fails. In this setting, I am running the code from a service. The code is the exact same - but for some reason it won't download any files. Would this have to do with the fact that it is running on a service instead of an activity? Or maybe for some reason the file output stream gets messed up.
In BGService:
String name = remote_resource.getValue().substring(38);
downloadFromUrl(remote_resource.getValue(), name);
/**
* Downloads the picture from the URL to the SD card
* @param imageURL: the URL to download it from (absolute web URL)
* @param fileName: the name of the file you want to save the picture to
*/
public void downloadFromUrl(String imageURL, String fileName) {
try {
// Connect to the URL
URL myImageURL = new URL(imageURL);
HttpURLConnection connection = (HttpURLConnection)myImageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
// Get the bitmap
Bitmap myBitmap = BitmapFactory.decodeStream(input);
// Save the bitmap to the file
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, fileName);
Log.i("help", file.getAbsolutePath());
Log.i("help", file.toString());
Log.i("help", file.length() + "");
System.out.println("THIS IS A TEST OF SYSTEM.OUT.PRINTLN()");
fOut = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
System.out.println("file Path: " + file.getAbsolutePath());
Log.i("help2", file.getAbsolutePath());
Log.i("help2", file.toString());
Log.i("help2", file.length() + "");
} catch (IOException e) {}
}
Another interesting thing is that of the "Log.i" calls below, only the second one (below the fOut.flush()) gets printed. So somewhere in the bitmap compression or the FileOutputStream IO something weird is happening. But no exceptions are thrown.
Thanks a lot for your help!