我有将图像保存到媒体库的方法,但我总是返回null,这是我将图像保存到图库的代码
private void downloadPictureToGallery(String imageToSave){
this.activity = (MyActivity) this.getActivity(); // Context
CustomDialogClass cdd=new CustomDialogClass(this.activity);
String saveSuccess = MediaStore.Images.Media.insertImage(this.activity.getContentResolver(), setDownloadPicture(imageToSave), "Title" , "Description");
if(saveSuccess != null){
Log.d(Tag,"Image saved!!");
}else{
Log.d(Tag,"Image could not save to image gallery");
}
cdd.show();
}
这是我将图像转换为位图文件的方法
private Bitmap bitmapFile(final String PageName){
String name = PageName;
this.activity = (MagazineActivity) this.getActivity(); // Context
AssetManager assetManager = this.activity.getAssets();
InputStream istr = null;
try {
istr = assetManager.open("books/" + activity.getJsonBook().getMagazineName() + "/" + name);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
这是我的日志错误
01-28 18:47:31.638 1094-1204/com.baker.abaker E/MediaStore﹕ Failed to insert image
java.io.FileNotFoundException: No such file or directory
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:611)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:922)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:669)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:645)
at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:902)
at com.baker.abaker.views.WebViewFragment.downloadPictureToGallery(WebViewFragment.java:478)
at com.baker.abaker.views.WebViewFragment.access$400(WebViewFragment.java:80)
at com.baker.abaker.views.WebViewFragment$WebAppInterface.receiveString(WebViewFragment.java:558)
at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
感谢您的帮助。
答案 0 :(得分:1)
使用此方法保存图像。 1.首先将图像转换为Bitmap,然后调用此方法。
public void saveBitmap(Bitmap bmp) {
String _time = "";
Calendar cal = Calendar.getInstance();
int millisecond = cal.get(Calendar.MILLISECOND);
int second = cal.get(Calendar.SECOND);
int minute = cal.get(Calendar.MINUTE);
int hourofday = cal.get(Calendar.HOUR_OF_DAY);
_time = "image_" + hourofday + "" + minute + "" + second + ""
+ millisecond + ".png";
String file_path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/your_folder_name";
try {
File dir = new File(file_path);
if (!dir.exists())
dir.mkdirs();
File file = new File(dir, _time);
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, fOut);
fOut.flush();
fOut.close();
Toast.makeText(getApplicationContext(),
"Image has been saved in KidsPainting folder",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("error in saving image", e.getMessage());
}
}