保存/载入图像/从本地存储 [英] Save / Load image to/from local storage

查看:144
本文介绍了保存/载入图像/从本地存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的权限的:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我的方法的保存的和的获得的图像:

private void saveIamgeToLocalStore(Bitmap finalBitmap) { 
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/temp");    
        myDir.mkdirs(); 
        String fname = "Profile_Image.png";
        File file = new File (myDir, fname);
        if (file.exists()) file.delete(); 
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close(); 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void loadImageFromLocalStore(String imageURI) { 
        try {
            Uri uri = Uri.parse(Environment.getExternalStorageDirectory().toString() + imageURI); 
            Bitmap bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri));
            profileImage.setImageBitmap(bitmap);
            profileImage.setTag("Other");
            select_image_button.setText(R.string.button_remove_profile_picture); 
        } catch (FileNotFoundException e) { 
            e.printStackTrace();
        } 
    }

用法:

saveIamgeToLocalStore(BitmapFactory.decodeFile(picturePath));
loadImageFromLocalStore("/temp/Profile_Image.png");

我收到一个

java.io.FileNotFoundException: No content provider: ... 

警告。

我是什么失踪?

PS:图像获取保存在的/ mnt / SD卡/温度/ 。加载图像时会出现警告。

PS: The image gets saved in /mnt/sdcard/temp/ . The warning appears when loading the image.

推荐答案

是您的文件中获取已保存?如果是的,可能是你做的文件的读取之前的mediascanner不会被触发。由于mediascanner未被触发,因此内容提供商不会有入门
    为您的文件(文件不被索引)。在情况下,你的文件得到保存saveIamgeToLocalStore,然后触发code曾这样mediascanner:

Is your file getting saved? In case yes, may be the mediascanner is not triggered before you do a read of the file. Since the mediascanner is not triggered, so content provider wont have the entry for your file (your file is not indexed).In case your file is getting saved with "saveIamgeToLocalStore", then trigger mediascanner from code once like this:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
                        .parse("file://"
                                + Environment.getExternalStorageDirectory())));

,然后做对文件的读。它应该工作。

and then do a read on the file. It should work.

这篇关于保存/载入图像/从本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆