简体   繁体   English

将图像保存到媒体库,返回null

[英]Saving image to media gallery returning null

I got method to save image to media gallery, but I always get returning null, here is my code to save image to gallery 我有将图像保存到媒体库的方法,但是我总是返回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();

}

And here my method to convert image to bitmap file 这是我将图像转换为位图文件的方法

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;


}

And here my log error 这是我的日志错误

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)

Thanks for your help. 谢谢你的帮助。

Use This Method to save image. 使用此方法保存图像。 1.First convert that image into Bitmap then call this method. 1.首先将该图像转换为位图,然后调用此方法。

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());
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM