1

I am trying to set the image of my imageButtons when a user is selecting a photo from the gallery or is taking a photo using the camera. The problem is that my imageButtons are just changed to a blank image, but I am getting the file directory. What am I doing wrong? I have created my ImageIntent and onActivityResult from this answer camera & gallery. But here is my onActivityResult method:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_IMAGE_CAPTURE) {
            final boolean isCamera;
            if (data == null) {
                isCamera = true;
            } else {
                final String action = data.getAction();
                if (action == null) {
                    isCamera = false;
                } else {
                    isCamera = action
                            .equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }

            Uri selectedImageUri;
            if (isCamera) {
                selectedImageUri = outputFileUri;
            } else {
                selectedImageUri = data == null ? null : data.getData();
            }

            ImageButton pic1 = (ImageButton) findViewById(R.id.ibPic1);
            Toast.makeText(this, "Image saved to:\n" + selectedImageUri,
                    Toast.LENGTH_LONG).show();
            pic1.setImageURI(selectedImageUri);
        }
    }
}

So i know from the Toast that I am getting the Uri's. I have tried this answer, and various other solutions which involves some sort of Bitmap, but these are always resulting in app crash and out of memory exceptions.

Edit

OnClick method to launch the image intent:

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.ibPic1:
        openImageIntent();
        break;
    }
}

Image intent method

private void openImageIntent() {

    // Determine Uri of camera image to save.
    final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "Klea" + File.separator);
    root.mkdirs();
    final String fname = Sell.getUniqueImageFilename();
    final File sdImageMainDirectory = new File(root, fname);
    outputFileUri = Uri.fromFile(sdImageMainDirectory);

    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
            captureIntent, 0);
    for (ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName,
                res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(intent);
    }
    // Filesystem.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent,
            "Vælg kilde");

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
            cameraIntents.toArray(new Parcelable[] {}));

    startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE);
}

Get unique file name method:

private static String getUniqueImageFilename() {
    // TODO Auto-generated method stub
    String fileName = "img_" + System.currentTimeMillis() + ".jpg";
    return fileName;
}
Community
  • 1
  • 1
Kæmpe Klunker
  • 865
  • 1
  • 10
  • 27
  • Are both gallery and camera not working? Can you check for both? – berserk Oct 28 '14 at 18:43
  • Neither are working. The toast says "file://....." for camera and "content://......" for gallery. – Kæmpe Klunker Oct 28 '14 at 18:45
  • In camera, u will get exact uri, but in case of gallery, you are getting Content Uri. – berserk Oct 28 '14 at 18:46
  • But still none of them are setting the image to other than blank. There is a picture before I insert the new one, and i can set another picture after with a button click, so I don't think there's a mistake in that matter. – Kæmpe Klunker Oct 28 '14 at 18:48
  • Not sure why it is not working. But I can provide an alternate. Convert the image into bitmap and then set bitmap. – berserk Oct 28 '14 at 18:58
  • I will definetely have a look at it, if it doesn't have the out of memory errors i have experienced from other solutions. I should maybe clarify that I need up to 4 pictures, and the user is able to remove a picture and take a new one to set in the imagebutton. – Kæmpe Klunker Oct 28 '14 at 19:10

2 Answers2

1

Its an alternative by creating bitmap from file and setting it. I also include conversion from content uri to actual uri(as to post file, you need actual uri) and image sampling to avoid OOM:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
    if (requestCode == REQUEST_IMAGE_CAPTURE) {
        final boolean isCamera;
        if (data == null) {
            isCamera = true;
        } else {
            final String action = data.getAction();
            if (action == null) {
                isCamera = false;
            } else {
                isCamera = action
                        .equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            }
        }

        Uri selectedImageUri;
        if (isCamera) {
            selectedImageUri = outputFileUri;
        } else {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = activity.getContentResolver().query(
                    selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            selectedImageUri = cursor.getString(columnIndex);
            cursor.close();
        }

        ImageButton pic1 = (ImageButton) findViewById(R.id.ibPic1);
        Toast.makeText(this, "Image saved to:\n" + selectedImageUri,
                Toast.LENGTH_LONG).show();
        //.setImageURI(selectedImageUri);
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(selectedImageUri, options);
        options.inSampleSize = calculateInSampleSize(options, dpToPx(100),
                dpToPx(100));

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bMapRotate = BitmapFactory.decodeFile(filePath, options);
        int width = bMapRotate.getWidth();
        int height = bMapRotate.getHeight();
        if (width > height)
            para = height;
        else
            para = width;
        if (bMapRotate != null) {
            pic1.setImageBitmap(bMapRotate);
        }
    }
}

private int dpToPx(int dp) {
    float density = activity.getResources().getDisplayMetrics().density;
    return Math.round((float) dp * density);
}

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and
        // width
        final int heightRatio = Math.round((float) height
                / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}
berserk
  • 2,690
  • 3
  • 32
  • 63
  • It is working, but not when I'm using the camera, this is how i get the: outputFileUri = Uri.fromFile(sdImageMainDirectory); and i changed the: else if (requestCode == CAMERA_REQUEST && resultCode == -1) { filePath= outputFileUri; to outputFileUri.toString(); Also I don't know if it means anything that i don't have a CAMERA_REQUEST code, since i'm launching the openImageIntent from one intent to chose from camera or other image libraries. Do you want me to post the intent method? – Kæmpe Klunker Oct 28 '14 at 20:07
  • @KæmpeKlunker Just need to modify a little according to you. Like you used boolean to check whether camera or gallery, I check request code. CAMERA_REQUEST is just a number for camera requestCode. – berserk Oct 28 '14 at 20:13
  • yes i know that, but you have both RESULT_LOAD_IMAGE and CAMERA_REQUEST, where i have: startActivityForResult(chooserIntent, int); so I don't know how I can define each number? You can check my questions first link "camera & gallery" to see the code, its the most upvoted answer. – Kæmpe Klunker Oct 28 '14 at 21:18
  • I'm not quite sure why this is throwing a null pointer exception in the bMapRotate.getWidth(); both for camera and gallery. – Kæmpe Klunker Oct 29 '14 at 17:35
  • @KæmpeKlunker Maybe the bitmap becoming is null. Check if the file path exists or not/ – berserk Oct 29 '14 at 18:51
  • It does, and it actually works for gallery if I use the setImageURI, but it doesn't work with the camera. I guess I'll have to play around with it for some time. – Kæmpe Klunker Oct 29 '14 at 19:32
  • @KæmpeKlunker you mean in your original code or in my code? – berserk Oct 29 '14 at 21:10
  • it also works with your code now, I just have the problem that when I take pictures with the camera it doesn't save the picture anymore. I don't know what went wrong there, coz it saved them earlier. So naturally it can't load the image from the camera since it's not saved. – Kæmpe Klunker Oct 29 '14 at 21:35
  • okay I am gonna accept your answer now, since the only bug I have to get rid of now, is that the camera takes +30 sec to create the image in the gallery. – Kæmpe Klunker Oct 30 '14 at 06:50
  • I have just added the code. Yes it takes extremely long time. After taking the picture i go to the gallery to check if its there since my app says its not, and it isn't. I also have the gallery folder open on my pc. Then after some waiting it suddenly appears in the folder. – Kæmpe Klunker Oct 30 '14 at 08:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63916/discussion-between-berserk-and-kaempe-klunker). – berserk Oct 30 '14 at 09:18
0

Maybe you had the same problem as me, as I understood xml properties like (app:tint="@color/white") or others in your case in the layout file were hiding my image.

After these lines setImageUri works:

binding.image.setImageTintMode(null);
binding.image.setImageURI(uri);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
RasulOs
  • 41
  • 4