如何使用imageview onclick保存位图图像

时间:2012-01-31 11:27:15

标签: android android-gallery

在android中开发图像着色。因此,当我点击另一个图像视图(例如保存)时将颜色应用于我的图像后,我必须将该图像保存到图库。

3 个答案:

答案 0 :(得分:19)

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) {
}

答案 1 :(得分:3)

你必须

  1. 将图像保存到持久存储中。
  2. MediaStore内容提供商添加条目。
  3. 首先可以使用以下代码实现:

    FileOutputStream out = new FileOutputStream(filePath);
    bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
    

    其次,

    MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, imagePath, name, description);
    

答案 2 :(得分:-1)

首先获取imageView的drawingCache(位图),然后将位图保存到SD卡。

File folder = new File(Environment.getExternalStorageDirectory()+“/ folder /”);     if(!folder.exists())folderAppointment.mkdirs();

try {
    this.setDrawingCacheEnabled(true);
    FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/folder/file"));
    Bitmap bitmap = YOUR_IMAGE_VIEW.getDrawingCache();
    bitmap.compress(CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}