我如何以我想要的方式使用onActivityResult

时间:2014-06-04 10:49:05

标签: android image android-intent bitmap onactivityresult

我正在自己学习android / java @the moment,我对我正在学习的应用程序的一部分有疑问。

我在www的帮助下制作了代码,我的问题是,如果我从图库中打开图像,它会发送到编辑活动,但图片的质量非常糟糕。

我认为主要问题的代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case PICK_IMAGE_FROM_GALLERY:
            {
                if (resultCode == RESULT_OK)
                {
                        Log.d(TAG, "Got Picture!");
                        Log.d(TAG,"File type - " + data.getType());
                        Uri photoUri = data.getData();
                        if (photoUri != null)
                        {
                            try {
                                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                                String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
                                int orientation = -1;
                                Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
                                cursor.moveToFirst();
                                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                                String filePath = cursor.getString(columnIndex);
                                cursor.close();
                                cursor = getContentResolver().query(photoUri, orientationColumn, null, null, null);
                                if(cursor != null && cursor.moveToFirst()){
                                    orientation = cursor.getInt(cursor.getColumnIndex(orientationColumn[0]));
                                }
                                cursor.close();

                                HashMap<String, Integer> pRes = this.getImageResolutionSetting();
                                Bitmap shrunkenBitmap = FileUtilsHelper.shrinkBitmap(filePath, pRes.get("width"), pRes.get("height"));
                                shrunkenBitmap = rotateBitmapToOrientation(shrunkenBitmap, orientation);
                                String res = FileUtilsHelper.saveBitmapAsJpeg(shrunkenBitmap, this);
                                Log.d(TAG,"File Path: " + res);
                                shrunkenBitmap.recycle();
                                Intent editImage = new Intent(this, EditImage.class);
                                editImage.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
                                editImage.putExtra("stuff.path", res);

                                startActivity(editImage);
                            }catch(Exception e){
                                Toast.makeText(this, R.string.cant_save_image,Toast.LENGTH_SHORT).show();

                            }

                        }

                }
            }
            break;
            }
}}

我现在想要重写完整的onActivityResult,所以它使用1:1来自图库的图片或删除部分: shrinkBitmap(我认为这是质量差的主要问题) rotateBitmapToOrientation(因为它总是以错误为导向)

但我不知道这是怎么回事......

如果我删除那部分不再有效,我真的需要一位老师:)

谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

在filePath中,您拥有文件路径。因此,如果你传递它(通过putExtra())而不是res,你将编辑原始图像。

相关问题