Question

I have used following method to get image after calling photo gallery intent. I want to start another activity by passing the bitmap of that image and show that image in the second activity. But, nothing will happen after a photo is picked from photo gallery.

protected void onActivityResult(int requestCode, int resultCode, Intent
                    data) {
        final String path;

        if (requestCode == GALLERY_REQUEST) {
        if (resultCode == Activity.RESULT_OK) {

        Uri imageFileUri = data.getData();

            if (imageFileUri != null) {
                try {
                path = getPath(imageFileUri);
                BitmapFactory.Options load_option = new BitmapFactory.Options();
                load_option.inPurgeable = true;
                load_option.inDensity = 0;
                load_option.inTargetDensity = 0;
                load_option.inDensity = 0;
                load_option.inScaled = false;
                bmp_main = BitmapFactory.decodeFile(path, load_option);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmp_main.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();

                Intent intentPhoto = new Intent(MainActivity.this, SecondActivity.class);
                intentPhoto.putExtra("image",byteArray);
                startActivity(intentPhoto);

                    } catch (Exception e) {

                    e.printStackTrace();
                    }
                }
            }

            }


        }

Can anybody tell what's the problem?

N.B.: 1.I have added the activity in the manifest

2. There is no logcat error or exception regarding this

3. I have done debugging, it goes upto startActivity line correctly, but after that nothing happen

Was it helpful?

Solution

Use below code...

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri contentUri = data.getData();
        startActivity(new Intent(this, ViewGallery_Photo.class)
                .setData(contentUri));
    }

So in onActivityResult there are 2 logic

1) if you are loading images from gallery then it will take you to other activity

2) if you are capturing image from camera then it will take you to other activity not the same activity which is calling by gallery ...

OTHER TIPS

Call this out of your if condition:

Intent intentPhoto = new Intent(MainActivity.this, SecondActivity.class);
intentPhoto.putExtra("image",byteArray);
startActivity(intentPhoto);

Try this:

         try {
            Uri selectedImage = data.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 picturePath = cursor.getString(columnIndex);
            cursor.close();

            String fullPath = Environment.getExternalStorageDirectory()
                    .getAbsolutePath();
            try {

                File dir = new File(fullPath);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                OutputStream fOut = null;
                File file = new File(fullPath, "userImage" + ".png");
                if (file.exists())
                    file.delete();
                file.createNewFile();
                fOut = new FileOutputStream(file);
                fOut.flush();
                fOut.close();
                Log.v("Image saved", "in" + file);
            } catch (Exception e) {
                Log.e("saveToExternalStorage()", e.getMessage());
            }

            decodeFile(picturePath);
            /*
             * iv_display.setImageBitmap(mPhoto); Bitmap useThisBitmap =
             * Bitmap.createScaledBitmap(mPhoto, mPhoto.getWidth(),
             * mPhoto.getHeight(), true);
             */

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            myBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            bytepicture = baos.toByteArray();

            Intent newdata = new Intent(MainMenu.this, SecondActivity.class);
            newdata.putExtra("picture", bytepicture);
            startActivity(newdata);
        } catch (Exception e) {
            // TODO: handle exception
            Log.v("TAG", "No Image Selected:");
        }

For decode file :

 public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 3;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    mPhoto = BitmapFactory.decodeFile(filePath, o2);
    myBitmap = ExifUtils.rotateBitmap(filePath, mPhoto);

    // image.setImageBitmap(bitmap);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top