如何在拍摄照片后从ImageView中保存图像

3

我想从相册中选择图片或使用相机拍摄照片。 如果我将图片加载到imageView中,然后点击确认按钮,如何保存该图片? 我需要使用saveState()吗? 请发表一些评论。 谢谢。

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
     if (resultCode != RESULT_OK) return;
       switch (requestCode)
       {
        case PICK_FROM_CAMERA:
        Bitmap selectedImage = (Bitmap) data.getExtras().get("data");
        selectedImage = Bitmap.createScaledBitmap(selectedImage, 80, 80, false);
        mImageView.setImageBitmap(selectedImage); 
        break;

        case PICK_FROM_GALLERY:
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            mImageView.setImageURI(selectedImageUri);
        break;
       }
}

private void saveState() 
{
    String name = (String) nameEdit.getText().toString();
    String category = (String) categoryEdit.getText().toString();
    String expired_date = (String) expired_Date_Btn.getText().toString();
    ImageView image = (ImageView) mImageView.setImageURI(); //how to edit?
    if(mRowId == null)
    {
        long id = mDbHelper.insertItem(category, name, expired_date);
        if(id>0)
        {
            mRowId = id;
        }           
    }
    else 
    {
        mDbHelper.updateItem(mRowId, category, name, expired_date);
    }
}

//How can I save image after clicking button?
confirmButton.setOnClickListener(new View.OnClickListener() 
{
    public void onClick(View v){
       setResult(RESULT_OK);
       finish();
     }
}); 
2个回答

2

我不确定你为什么想从相册做这件事,因为如果图片在相册中,它已经保存在手机上了。但是你可以使用文件的URI重新编写文件。如果你正在使用相机拍摄照片,你可以看到你有一张位图图像。使用以下代码段应该相对容易保存它:

outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();

您需要获取权限

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

如需更多信息,请尝试按照此示例操作(这是我找到代码片段的地方)。他们正在上传自己的图片,但相同的概念应该适用。 http://android-er.blogspot.com/2010/07/save-file-to-sd-card.html

希望能对您有所帮助!


我的问题是我有一个项目列表,当我点击项目时,页面会跳转到编辑页面。在此页面上,我从相机和图库中拍摄照片。然后,在单击确认按钮后,我将页面返回到项目列表。图片已经存在,如何设置imageURI? - wholee1

2
你可以按照以下步骤保存所有视图(不仅仅是ImageView)的图像:
1. 获取你的视图的位图:
public Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.draw(c);
    v.invalidate();
    return b;
}

2.将它保存在你的SD卡文件中(或任何你想要的地方):

protected String saveBitmap(Bitmap bm, String name) throws Exception {
    String tempFilePath = Environment.getExternalStorageDirectory() + "/"
            + getPackageName() + "/" + name + ".jpg";
    File tempFile = new File(tempFilePath);
    if (!tempFile.exists()) {
        if (!tempFile.getParentFile().exists()) {
            tempFile.getParentFile().mkdirs();
        }
    }

    tempFile.delete();
    tempFile.createNewFile();

    int quality = 100;
    FileOutputStream fileOutputStream = new FileOutputStream(tempFile);

    BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
    bm.compress(CompressFormat.JPEG, quality, bos);

    bos.flush();
    bos.close();

    bm.recycle();

    return tempFilePath;
}

这些代码来自我的一个项目,但我认为它们易于理解和重用。希望能对你有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接