保存和读取位图/从内部存储器中的Andr​​oid图片 [英] Saving and Reading Bitmaps/Images from Internal memory in Android

查看:128
本文介绍了保存和读取位图/从内部存储器中的Andr​​oid图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是保存图片到手机(不是SD卡)的内部存储器。我该怎么办呢? 我已经得到了直接将图像从相机在我的应用程序的所有工作精细的图像显示。现在,我要的是从图像查看该图片保存到我的Andr​​oid设备的内部存储器,并在需要时访问它。任何人都可以请指导我如何做到这一点?

What I want to do is to save image to the internal memory of the phone (NOT SD CARD). How can I do it? I have got the image directly from the camera to the image view in my app its all working fine. Now what I want is to save this image from Image View to the Internal memory of my android device and also access it when required. Can anyone please guide me how to do this?

我有点新至Android所以请我就AP preciate如果我能有一个详细的过程。

I am a little new to android so please I would appreciate if I can have a detailed procedure.

推荐答案

使用下面的code将图像保存到内部目录。

Use the below code to save the image to internal directory.

private String saveToInternalSorage(Bitmap bitmapImage){
        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,"profile.jpg");

        FileOutputStream fos = null;
        try {           
            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);
        } catch (Exception e) {
              e.printStackTrace();
        } finally {
              fos.close(); 
        } 
        return directory.getAbsolutePath();
    }

说明:

1,目录将与给定的名称创建。 Javadoc中是要告诉在什么地方它会创建的目录。

1.The Directory will be created with the given name. Javadocs is for to tell where exactly it will create the directory.

2。你将不得不放弃以要保存的图像名称。

2.You will have to give the image name by which you want to save it.

要读取内部存储器中的文件。使用下面code

To Read the file from internal memory. Use below code

private void loadImageFromStorage(String path)
{

    try {
        File f=new File(path, "profile.jpg");
        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
            ImageView img=(ImageView)findViewById(R.id.imgPicker);
        img.setImageBitmap(b);
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }

}

这篇关于保存和读取位图/从内部存储器中的Andr​​oid图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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