[英]Downloading Image to SD Card in Service fails (Android)
我創建了一個單獨的項目和活動,該活動和活動從URL下載圖像,將其轉換為Bitmap,然后使用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中的某處發生了奇怪的事情。 但是不會拋出異常。
非常感謝你的幫助!
確定要啟用WRITE_EXTERNAL_STORAGE嗎? 如果您使用的是權限組.STORAGE,則不一定總是在其中包含此權限。
我用您的代碼創建了一個新項目,並且工作正常!
我在AndroidManifest.xml中添加了以下內容:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.