Android内存中位图/图像的保存与读取

Android内存中位图/图像的保存与读取

我想要做的是,把图像保存到电话的内存中。(不是SD卡).

我该怎么做?

我已经把图像直接从相机到我的应用程序中的图像视图,这一切都工作得很好。

现在我想要的是将这张图像从图像视图保存到我的Android设备的内部内存中,并在需要时访问它。

有人能指点我怎么做吗?

我是一个有点新的Android,所以请,我会感谢如果我可以有一个详细的程序。


交互式爱情
浏览 755回答 3
3回答

泛舟湖上清波郎朗

使用下面的代码将图像保存到内部目录。private String saveToInternalStorage(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 {             try {               fos.close();             } catch (IOException e) {               e.printStackTrace();             }         }          return directory.getAbsolutePath();     }说明:1.目录将使用给定的名称创建。Javadocs是用来告诉它将在哪里创建目录的。2.您必须给出要保存它的图像名称。若要从内部内存读取文件,请执行以下操作。使用以下代码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();     }}

皈依舞

今天遇到了这个问题,我就是这样做的。只需使用所需的参数调用此函数即可。public void saveImage(Context context, Bitmap bitmap, String name, String extension){     name = name + "." + extension;     FileOutputStream fileOutputStream;     try {         fileOutputStream = context.openFileOutput(name, Context.MODE_PRIVATE);         bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);         fileOutputStream.close();     } catch (Exception e) {         e.printStackTrace();     }}同样,要阅读相同的内容,请使用以下命令public Bitmap loadImageBitmap(Context context,String name,String extension){     name = name + "." + extension    FileInputStream fileInputStream    Bitmap bitmap = null;     try{         fileInputStream = context.openFileInput(name);         bitmap = BitmapFactory.decodeStream(fileInputStream);         fileInputStream.close();     } catch(Exception e) {         e.printStackTrace();     }      return bitmap;}
打开App,查看更多内容
随时随地看视频慕课网APP