1

I have made a feature for my android app which allow users to browse and import images from the gallery. But the app always crashes when a normal or a larger size image is imported but it does not crash when I import very small images. I want the user to be able to import normal sized images with ease and good speed. Here is my code:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_in_card);
    openGallery();
}

private void openGallery () {
        Intent intent = new Intent();
        // Show only images, no videos or anything else
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        // Always show the chooser (if there are multiple options available)
        startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.str_select_pic_gallery)), PICK_IMAGE_REQUEST);
    }


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

            Uri uri = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                setPicture(bitmap);
                // Log.d(TAG, String.valueOf(bitmap));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

@TargetApi(android.os.Build.VERSION_CODES.JELLY_BEAN)
private void setPicture (Bitmap b) {
            BitmapDrawable drawable;
            if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                drawable = new BitmapDrawable(getResources(), b);
                wholeImage.setBackgroundDrawable(drawable);
            } else {
                drawable = new BitmapDrawable(getResources(), b);
                wholeImage.setBackground(drawable);
            }
    }

However it does not crash in the emulator but it does on every phone. So I can't find the error code since its not being printed in the log cat.

Shahabas Shakeer
  • 241
  • 1
  • 3
  • 13

3 Answers3

1

Too large bitmap size result in your crash error.My solution is :

ContentResolver resolver = getContentResolver();
BitmapFactory.Options options = new  BitmapFactory.options();
options.inJustDecodeBounds = false;
options.inSampleSize = 4; // now you resize bitmap as 0.25 as before
Bitmap mBitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(mPhotoUri),null,options);

Hope it can help you.

0

You can use BitmapFactory. Its options can be used for bitmap size

0

try to swap openGallery() method against :

public void openGallery(View view) {
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}

... delete the openGallery() method in onCreate();

NTsola
  • 61
  • 1
  • 5