0
我需要將Drawable保存到文件,然後在需要時加載。問題是從函數生成的位圖一旦保存就變成黑色圖片。任何人都可以幫助我嗎?我已經看過幾個類似的問題,但沒有提供的答案與我的問題有關。Android:將位圖保存爲黑色圖片
轉換和文件功能:
public static Bitmap drawableToBitmap(Drawable d) {return Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);}
public static boolean saveDrawableToFile(File dir, String fileName, Drawable d, Bitmap.CompressFormat format, int quality) {
return saveBitmapToFile(dir, fileName, drawableToBitmap(d), format, quality);
}
public static boolean saveBitmapToFile(File dir, String fileName, Bitmap bm, Bitmap.CompressFormat format, int quality) {
File imageFile = new File(dir,fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(imageFile);
bm.compress(format,quality,fos);
fos.close();
return true;
}
catch (IOException e) {
Log.e("FileSaver",e.getMessage());
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return false;
}
代碼的活動:
File file = new File(getActivity().getCacheDir(),picture.getSamplePath());
if(file.exists()){
//Loads local file
Log.d(LOG_TAG, "Loading thumbnail " + file.getAbsolutePath());
image = Drawable.createFromPath(file.getAbsolutePath());
}else{
//Loads url
Log.d(LOG_TAG, "Downloading thumbnail " + urls[0]);
image = QBooruUtils.loadDrawableFromUrl(urls[0]);
//Saves drawable
Log.d(LOG_TAG, "Saving thumbnail " + file.getAbsolutePath());
QBooruUtils.saveDrawableToFile(getActivity().getCacheDir(),picture.getSamplePath(),image, Bitmap.CompressFormat.JPEG,100);
}
loadDrawableFromUrl只是旨在從遠程圖片下載繪製一個功能,它正確返回一個有效的可繪製。
登錄:
03-06 21:38:04.908 9418-9751/fr.fusoft.qbooru D/PicViewerFrag: Downloading thumbnail http://konachan.com/sample/6e27694cff3910adb4e2e0f1f0fe96dc/Konachan.com%20-%20216418%20sample.jpg
03-06 21:38:05.072 9418-9464/fr.fusoft.qbooru D/OpenGLRenderer: endAllStagingAnimators on 0xb7563a60 (GridView) with handle 0xb767d510
03-06 21:38:05.083 9418-9418/fr.fusoft.qbooru I/Timeline: Timeline: Activity_idle id: [email protected] time:340520855
03-06 21:38:06.147 9418-9751/fr.fusoft.qbooru D/PicViewerFrag: Saving thumbnail /data/data/fr.fusoft.qbooru/cache/Konachan_216418.jpg
03-06 21:38:08.900 9418-9868/fr.fusoft.qbooru D/PicViewerFrag: Loading thumbnail /data/data/fr.fusoft.qbooru/cache/Konachan_216418.jpg
哦,是的,我忘了用Canvas來填充它。我使用http://stackoverflow.com/a/10600736/6026551完成了它。感謝您的幫助,現在工作正常! –