如何从ImageView保存图像?

时间:2014-01-13 07:09:06

标签: android imageview

有想象力想将它保存到内存这里是我的代码:

View content = findViewById(R.id.full_image_view);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
    root.createNewFile();
    FileOutputStream ostream = new FileOutputStream(root);
    bitmap.compress(CompressFormat.JPEG, 100, ostream);
    ostream.close();
} catch (Exception e) {
    e.printStackTrace();
}


}

保存没有任何反应并且没有图像存在?

5 个答案:

答案 0 :(得分:1)

试试这个..

root.createNewFile();更改为cachePath.createNewFile();

File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
    cachePath.createNewFile();
    FileOutputStream ostream = new FileOutputStream(cachePath);
    bitmap.compress(CompressFormat.JPEG, 100, ostream);
    ostream.close();
} catch (Exception e) {
    e.printStackTrace();
}

修改

FileOutputStream ostream = new FileOutputStream(cachePath);

答案 1 :(得分:1)

使用此功能保存在SD卡中:

private void SaveIamge(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}

并添加清单:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

答案 2 :(得分:0)

如何使用以下代码段?

Drawable d = imageView.getBackground();
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

File file = new File(Environment.getExternalStorageDirectory(), "fileName.ext");
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();

答案 3 :(得分:0)

1 - 您需要在amnifest中获得适当的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2- out.flush()检查out是否为空..

3 -

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                            "/yourforlderName";
    File dir = new File(file_path);
if(!dir.exists())
    dir.mkdirs();
    File file = new File(dir, "yourforlderName" + image + ".png");
    FileOutputStream fOut = new FileOutputStream(file);

    bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
    fOut.flush();
    fOut.close();

如果您没有在SD卡中创建目录,那么如何在特定位置的SD卡中存储图像? 所以请检查一下......我希望它对你有用。

答案 4 :(得分:0)

从imageview获取位图:

  imageview.buildDrawingCache();
    Bitmap bm=imageview.getDrawingCache();

将其保存在文件中:

 OutputStream fOut = null;
    Uri outputFileUri;
     try {
    File root = new File(Environment.getExternalStorageDirectory()
      + File.separator + "folder_name" + File.separator);
    root.mkdirs();
   File sdImageMainDirectory = new File(root, "myPicName.jpg");
    outputFileUri = Uri.fromFile(sdImageMainDirectory);
    fOut = new FileOutputStream(sdImageMainDirectory);
   } catch (Exception e) {
    Toast.makeText(this, "Error occured. Please try again later.",
      Toast.LENGTH_SHORT).show();
   }

   try {
    bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    fOut.close();
   } catch (Exception e) {
   }

添加清单:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />