我创建了一个单独的项目和活动,它从 URL 下载图像,将其转换为位图,然后使用 FileOutputStream 将该文件保存到 SD 卡。在单独的项目和独立活动中,这工作正常,我可以看到图像存储在 SD 卡上。
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) {}
}
}
但是,每当我尝试将代码移植到现有项目时,它都会失败。在此设置中,我从服务运行代码。代码完全相同 - 但由于某种原因,它不会下载任何文件。这是否与它在服务而不是活动上运行的事实有关?或者可能由于某种原因文件输出流被弄乱了。
在 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) {}
}
另一个有趣的事情是下面的“Log.i”调用,只有第二个(在 fOut.flush() 下面)被打印出来。所以在位图压缩或 FileOutputStream IO 的某个地方发生了一些奇怪的事情。但不会抛出异常。
非常感谢你的帮助!