Select gallery image in Android causing problem

I am using very simple code to select image from gallery, it works on my phone. but testing it on three to four phones (Galaxy S3, Tablet, etc.) it doesn't work.

Environment: if the image size I took or was in the gallery below 500KB then it works

Environment It Does'nt Work: if the image size I took or was in the gallery above 500kb then it works

ImageView myimg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Toast.makeText(this, UUID.randomUUID().toString(),
        // Toast.LENGTH_LONG).show();

        myimg = (ImageView) findViewById(R.id.imageView1);

        mybutton = (Button) findViewById(R.id.myButton);
        mybutton.setOnClickListener(this);

    }

    public void onClick(View v) {

        if (v == mybutton) {

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, 0);

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        switch (requestCode) {
        case 0:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = imageReturnedIntent.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex); // file path of
                                                                    // selected
                                                                    // image
                cursor.close();
                // Convert file path into bitmap image using below line.
                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);

                // put bitmapimage in your imageview
                myimg.setImageBitmap(yourSelectedImage);
            }
        }

    }

      

Can anyone please advise on how to handle this situation?

Any help would be appreciated.

+3


source to share





All Articles