保存并从本地存储中获取位图图像

时间:2014-10-01 23:50:45

标签: java android bitmap bitmapfactory android-bitmap

我正在获取一个人的脸谱资料图片,我想将其保存到我的本地存储空间中,以便以后可以恢复并显示。

我做了所有this链接说,但是我在BitmapFactory.decodeStream行上得到一个异常而且我看不出异常是什么,当我调试时,调试器直接进入& #34;返回null"我无法评估" e"变量

这是我的代码:

InternalFileHelper.cs:

public void saveToInternalSorage(File mypath, Bitmap bitmapImage){
        FileOutputStream fos = null;
        try {
            if (!mypath.exists()) {
                fos = new FileOutputStream(mypath);

                // Use the compress method on the BitMap object to write image to the OutputStream
                bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //return directory.getAbsolutePath();
    }

    public Bitmap loadImageFromStorage(File f)
    {
        try {
            if (f.exists()) {

                Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
                return b;
            }else
                return null;
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
            String mess = e.getMessage();
            return null;
        }
    }

以下是我保存图片的方法:

ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,this.fbId.toString()+".png");

InputStream inputStream;
HttpGet httpRequest = new HttpGet(URI.create("http://graph.facebook.com/" + this.fbId.toString() + "/picture?type=normal"));
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Bitmap bmp = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();

this.image = bmp;
helper.saveToInternalSorage(mypath, bmp);

以及我如何恢复图像:

ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,this.fbId.toString()+".png");

InternalFileHelper helper = new InternalFileHelper();
Bitmap image = helper.loadImageFromStorage(mypath);

它让我疯了!提前谢谢!!

0 个答案:

没有答案