java - 将图像保存到媒体库返回空

标签 java android

我有将图像保存到媒体库的方法,但我总是返回 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)

感谢您的帮助。

最佳答案

使用此方法保存图像。 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());
        }
    }

关于java - 将图像保存到媒体库返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28190459/

相关文章:

java - 将与会者添加到 android 日历事件

java - 在 fragment Android java中初始化ViewModel

android - ScrollView中如何滑动到imageView

php - 如何将字符串变量从我的 android 应用程序发布到 php 脚本

java - monad 的一般情况可以在 java 6 中表达吗?

java - 如何在 TestNG 中使用 Selenium Webdriver 使用文本属性来识别元素

java - postgresql 中的日期

android - 如何解决 android api 26 中的录制声音问题

java - 如何将操作栏菜单位置设置为右侧

JAVA:并发序列