简体   繁体   English

从图库应用程序中选择图像

[英]Selecting image from a gallery app

I was using this code to allow the user to choose an image from a gallery app and to get that image afterwards. 我正在使用此代码允许用户从图库应用中选择图像,然后再获取该图像。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == REQUEST_CAMERA) {
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                mImg.setImageBitmap(photo);
            } else if (requestCode == SELECT_FILE) {
                Uri selectedImageUri = data.getData();
                String[] projection = {MediaStore.MediaColumns.DATA};
                CursorLoader cursorLoader = new CursorLoader(getActivity(), selectedImageUri, projection, null, null,
                        null);
                Cursor cursor = cursorLoader.loadInBackground();
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                cursor.moveToFirst();

                String selectedImagePath = cursor.getString(column_index);

                Bitmap bm;
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(selectedImagePath, options);
                final int REQUIRED_SIZE = 200;
                int scale = 1;
                while (options.outWidth / scale / 2 >= REQUIRED_SIZE
                        && options.outHeight / scale / 2 >= REQUIRED_SIZE)
                    scale *= 2;
                options.inSampleSize = scale;
                options.inJustDecodeBounds = false;
                bm = BitmapFactory.decodeFile(selectedImagePath, options);

                mImg.setImageBitmap(bm);
                mImg.setAlpha(1);

            }
    }
}

But in this answer here I was told that this code wont work in most android devices. 但在这里的答案中我被告知该代码在大多数android设备上均不起作用。 Can I know why? 我能知道为什么吗? And what is the best way to get an image chosen by the user. 以及获得用户选择的图像的最佳方法是什么。

I posted this in a separate question because it might be interesting. 我将其发布在一个单独的问题中,因为它可能很有趣。

By using bitmap , your image may looked blurry. 通过使用bitmap ,您的图像可能看起来模糊。 If the image size too large, it will be a problem when you want to upload or retrieved them to(from) server. 如果图像尺寸太大,则要将图像上载或从服务器检索到图像时会出现问题。 So uri is better than bitmap . 所以uribitmap

You may try below code and you will see the difference between bitmap and uri 您可以尝试下面的代码,您将看到bitmapuri之间的区别

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
                    selectedImage = data.getData();
                    imageView.setImageURI(selectedImage);
                }
                else
                {
                    imageView.setImageResource(R.mipmap.no_image);
                }

                break;

            case REQUEST_IMAGE_CAPTURE:
                if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                    try {
                        selectedImage = imageUri;
                        getContentResolver().notifyChange(selectedImage, null);
                        imageView.setImageURI(null);
                        imageView.setImageURI(imageUri);
                    } catch (Exception e) {
                        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                                .show();
                        Log.e("Camera", e.toString());
                    }
                    Log.e("A", "AAA");
                }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM