【问题标题】:How to save an image from an ImageView after taking a picture拍照后如何从 ImageView 中保存图像
【发布时间】:2011-11-30 01:32:39
【问题描述】:

我正在从图库中挑选图片或用相机拍摄。 如果我将图片放入我的 imageView,然后单击确认按钮,我该如何保存该图片? 我必须使用 saveState() 吗? 请张贴一些cmets。 谢谢。

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

【问题讨论】:

    标签: android camera image savestate


    【解决方案1】:

    您可以按照以下步骤保存所有View(不仅仅是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;
    }
    


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

    【讨论】:

      【解决方案2】:

      我不确定如何从图库中执行此操作,或者您为什么要这样做,因为如果图像在图库中,则该图像已经保存到手机中。不过,您应该能够使用文件的 URI 重写文件。 如果您使用相机拍摄图像,您可以看到您拥有图像的位图。使用下面的sn-p代码保存起来应该比较容易:

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

      你会想要权限

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

      有关更多信息,请尝试按照此示例进行操作(我在这里找到了代码 sn-ps)。他们正在上传他们的图片,但同样的概念应该适用。 http://android-er.blogspot.com/2010/07/save-file-to-sd-card.html

      希望这会有所帮助!

      【讨论】:

      • 我的问题是我有一个项目列表,当我单击该项目时,页面转到编辑页面。在那之后,我从这个页面上的相机和画廊拍照。然后我会在点击确认按钮后返回页面到项目列表。图片已经存在,如何设置imageURI?
      猜你喜欢
      • 2014-01-22
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多