在Android中保存和读取来自内部存储器的位图/图像

我想要做的是将图像保存到手机的内部存储器(不是SD卡)

我该怎么做?

我已经从相机的图像直接到我的应用程序中的图像视图,它的工作正常。

现在我想要的是将图像从图像视图保存到我的android设备的内部存储器,也可以在需要时访问它。

任何人都可以请指导我如何做到这一点?

所以请给我一个新的android,如果我可以有一个详细的程序,我将不胜感激。

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

 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(); } } 
 /** * Created by Ilya Gazman on 3/6/2016. */ public class ImageSaver { private String directoryName = "images"; private String fileName = "image.png"; private Context context; private boolean external; public ImageSaver(Context context) { this.context = context; } public ImageSaver setFileName(String fileName) { this.fileName = fileName; return this; } public ImageSaver setExternal(boolean external) { this.external = external; return this; } public ImageSaver setDirectoryName(String directoryName) { this.directoryName = directoryName; return this; } public void save(Bitmap bitmapImage) { FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(createFile()); bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } @NonNull private File createFile() { File directory; if(external){ directory = getAlbumStorageDir(directoryName); } else { directory = context.getDir(directoryName, Context.MODE_PRIVATE); } if(!directory.exists() && !directory.mkdirs()){ Log.e("ImageSaver","Error creating directory " + directory); } return new File(directory, fileName); } private File getAlbumStorageDir(String albumName) { return new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), albumName); } public static boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); return Environment.MEDIA_MOUNTED.equals(state); } public static boolean isExternalStorageReadable() { String state = Environment.getExternalStorageState(); return Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state); } public Bitmap load() { FileInputStream inputStream = null; try { inputStream = new FileInputStream(createFile()); return BitmapFactory.decodeStream(inputStream); } catch (Exception e) { e.printStackTrace(); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } } 

用法

  • 保存:

     new ImageSaver(context). setFileName("myImage.png"). setDirectoryName("images"). save(bitmap); 
  • 加载:

     Bitmap bitmap = new ImageSaver(context). setFileName("myImage.png"). setDirectoryName("images"). load(); 

编辑:

添加了ImageSaver.setExternal(boolean)来支持基于Google的例子保存到外部存储。

今天遇到这个问题,我就是这样做的。 只需使用所需的参数调用此函数

 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); } catch (Exception e) { e.printStackTrace(); } finally { fileOutputStream.close(); } } 

同样的阅读相同的,使用这个

 public Bitmap loadImageBitmap(Context context,String name,String extension){ name = name + "." + extension; FileInputStream fileInputStream Bitmap bitmap; try{ fileInputStream = context.openFileInput(name); bitmap = BitmapFactory.decodeStream(bitmap); } catch(Exception e) { e.printStackTrace(); } finally { fileInputStream.close(); } return bitmap; } 

你将图像转换为字节…然后你可以将其存储到数据库,当你不会使用这个图片,你会重新从字节转换,,,找到如何将图像转换为字节… 。