Android Intent-从图库中选择图片

问题描述:

我正在遵循以下示例代码发送意图获得图像.

I am following this sample code to send an intent to get an image.

Intent i = new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);

i.setType("image/*");

if (i.resolveActivity(getPackageManager()) != null) {
     startActivityForResult(i, 3);
}

onActivityResult中:

if(requestCode == 3 && resultCode == RESULT_OK) {
     Bitmap thumbnail = data.getParcelableExtra("data");

     if(thumbnail == null) {
        Toast.makeText(getApplicationContext(), "Thumbnail is null", Toast.LENGTH_SHORT).show();
        return;
     }

     ImageView iv = (ImageView) findViewById(R.id.imageView);
     iv.setImageBitmap(thumbnail);
 }

发送意图后,图像选择器启动,然后我选择图库中的图像之一.但是在onActivityResult中,对应于示例代码中的以下语句(我在Android 4.4中更改为getParcelableExtra):

When the intent is sent, the image picker launches, and I select one of the images in the gallery. But in the onActivityResult, corresponding to the following statement in the sample code (I changed to getParcelableExtra in Android 4.4):

Bitmap thumbnail = data.getParcelable("data");

我得到的缩略图为空.我正在Genymotion VM上进行测试.

I get the thumbnail as null. I am testing on Genymotion VM.

我在做什么错了?

        btnFromGallery.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        try {
                            Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            Log.i("after select", "get image");
                            startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
                        } catch (Exception e) {
                            Toast.makeText(getApplicationContext(), R.string.imageException, Toast.LENGTH_LONG).show();
                            Log.e(e.getClass().getName(), e.getMessage(), e);
                        }
                    }
                });

    //onActivityResult code:

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {

            case PICK_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    Log.i("On onActivityResult", "on Activity Result");
                    Uri selectedImageUri = data.getData();
                    String filePath = null;

                    try {
                        String filemanagerstring = selectedImageUri.getPath();

                        selectedImagePath = getPath(selectedImageUri);
                        if (selectedImagePath != null) {
                            filePath = selectedImagePath;
                        } else if (filemanagerstring != null) {
                            filePath = filemanagerstring;
                        } else {
                            Toast.makeText(getApplicationContext(), R.string.unknownPath, Toast.LENGTH_LONG).show();
                        }

                        if (filePath != null) {
                            decodeFile(filePath);
                        } else {
                            bitmap = null;
                        }
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(), R.string.dataException, Toast.LENGTH_LONG).show();
                    }
                }
                break;

            }
        }


//Get Path Method

    public String getPath(Uri uri) {
            String[] projection = { MediaColumns.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } else
                return null;
        }

//Decode File method

public void decodeFile(String filePath) {
        try {

            File f = new File(filePath);
            ExifInterface exif = new ExifInterface(f.getPath());
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            int angle = 0;

            if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                angle = 90;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                angle = 180;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                angle = 270;
            }

            Matrix mat = new Matrix();
            mat.postRotate(angle);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2;

            Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
            bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
            ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstudentstreamOutputStream);
            imgEdit.setImageBitmap(bitmap);