简体   繁体   中英

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.

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.

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 . Generally camera roll captures images larger in size, if the image has any length and width sized more than 2048 , It require cropping.

  • Valid size to set on ImageView < 2048*2048 , from camera roll

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

Check if Uri is valid. 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

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

You can use this to scale down your image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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