为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Android内存中位图/图像的保存与读取我想要做的是,把图像保存到电话的内存中。(不是SD卡).我该怎么做?我已经把图像直接从相机到我的应用程序中的图像视图,这一切都工作得很好。现在我想要的是将这张图像从图像视图保存到我的Android设备的内部内存中,并在需要时访问它。有人能指点我怎么做吗?我是一个有点新的Android,所以请,我会感谢如果我可以有一个详细的程序。
查看完整描述

3 回答

?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超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();
    }}


查看完整回答
反对 回复 2019-06-20
?
皈依舞

TA贡献1851条经验 获得超3个赞

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

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;}


查看完整回答
反对 回复 2019-06-20
  • 3 回答
  • 0 关注
  • 751 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信