简体   繁体   English

为什么我无法从Android图库中的相机相册中获取图像?

[英]Why can't I get an image from the camera album in Android gallery?

I can get images from the gallery, but I can't get from the camera album/roll. 我可以从图库中获取图像,但不能从相册/胶卷中获取图像。 But I know I got the URI of an image from the camera roll and still it won't show up in my imageview. 但是我知道我从相机胶卷中获得了图像的URI,但它仍然不会显示在我的imageview中。

Opening the Gallery: 打开画廊:

if(resultCode == RESULT_OK){
        imgUri = data.getData();
        if(requestCode == PICK_IMAGE){
            Intent sendpic = new Intent(getApplicationContext(), GalleryChoice.class);
            sendpic.putExtra("imagePath", imgUri.toString());
            Log.d("SHOW UP", imgUri.toString());
            startActivity(sendpic);

Getting the URI from the previous activity to show up in the ImageView, where the image is not showing up. 从上一个活动中获取URI以显示在未显示图像的ImageView中。

if(getIntent().hasExtra("imagePath")){
        ur = getIntent().getStringExtra("imagePath");
        Uri newUri = Uri.parse(ur);
        try {
            bitmapy = MediaStore.Images.Media.getBitmap(this.getContentResolver(), newUri);
            imgView1.setImageBitmap(bitmapy);
        }
        catch (IOException e){

        }
  }

It generally happens when the Image is large is size which is 2048*2048 . 通常,当Image大小为2048*2048时,会发生这种情况。 Generally camera roll captures images larger in size, if the image has any length and width sized more than 2048 , It require cropping. 通常,相机胶卷会捕获尺寸较大的图像,如果图像的长度和宽度大于2048 ,则需要裁切。

  • Valid size to set on ImageView < 2048*2048 , from camera roll 从相机胶卷在ImageView < 2048*2048上设置的有效尺寸

Below is just an example to crop image if large in size,and does not take care of image ratio 下面仅是裁剪图像的示例(如果尺寸过大且不考虑图像比例)

    public class InputImageCompressor extends Activity{

    public static void compressInputImage(Uri inputImageData, Context context, ImageView newIV)
    {
        Bitmap bitmap;
        //Uri inputImageData = data.getData();
        try
        {
            Bitmap bitmapInputImage = MediaStore.Images.Media.getBitmap(context.getContentResolver(), inputImageData);
            if (bitmapInputImage.getWidth() > 2048 && bitmapInputImage.getHeight() > 2048)
            {
                bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1024, 1280, true);
                newIV.setImageBitmap(bitmap);
            }
            else if (bitmapInputImage.getWidth() > 2048 && bitmapInputImage.getHeight() < 2048)
            {
                bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1920, 1200, true);
                newIV.setImageBitmap(bitmap);
            }
            else if (bitmapInputImage.getWidth() < 2048 && bitmapInputImage.getHeight() > 2048)
            {
                bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1024, 1280, true);
                newIV.setImageBitmap(bitmap);
            }
            else if (bitmapInputImage.getWidth() < 2048 && bitmapInputImage.getHeight() < 2048)
            {
                bitmap = Bitmap.createScaledBitmap(bitmapInputImage, bitmapInputImage.getWidth(), bitmapInputImage.getHeight(), true);
                newIV.setImageBitmap(bitmap);
            }
        } catch (Exception e)
        {
            Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
        }
    }
}

Use this in your code: 在您的代码中使用它:

if(getIntent().hasExtra("imagePath")){
        ur = getIntent().getStringExtra("imagePath");
        Uri newUri = Uri.parse(ur);
        try {
            InputImageCompressor.compressInputImage(newUri, your_class_name.this, imgView1);
            }
        catch (IOException e){

        }
  }

Why image size limited to 2048*2048 read more 为什么图像尺寸限制为2048 * 2048 阅读更多

Check if Uri is valid. 检查Uri是否有效。 If it's, use method below to get it's real address. 如果是这样,请使用下面的方法获取它的真实地址。

public String getRealPathFromURI(Uri uri){
    String filePath = "";
    String[] filePahColumn = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(uri, filePahColumn, null, null, null);
    if (cursor != null) {
        if(cursor.moveToFirst()){
            int columnIndex = cursor.getColumnIndex(filePahColumn[0]);
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();
    }
    return filePath;
}

Use this filePath to set your image 使用此文件filePath设置图像

yourImageView.setImageBitmap(BitmapFactory.decodeFile(filePath));

You can use this to scale down your image. 您可以使用它来缩小图像。

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

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