0

Folks.

Do i need to do any extra step to view the Camera folder images? I have my application that has a dialogbox to choose from Camera or Gallery. Both are working fine except whenever i open any image from the camera folder it crashes and it opens perfectly with any other image in the gallery. Also the camera option works fine so that i can take and view images.

Please help me !! Its been a week trying to resolve this issue.

AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);

        builder.setTitle("Choose Image Source");
        builder.setItems(new CharSequence[] { "Gallery", "Camera" },
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                        case 0:

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

                            Intent chooser = Intent.createChooser(intent,
                                    "Choose a Picture");
                            startActivityForResult(chooser,
                                    ACTION_REQUEST_GALLERY);

                            break;

                        case 1:

                            Intent cameraIntent = new Intent(
                                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            String mImageCaptureUri = null;
                            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                    mImageCaptureUri);
                            startActivityForResult(cameraIntent,
                                    ACTION_REQUEST_CAMERA);

                            break;

                        }
                    }
                });

        builder.show();

        break;

    }

}

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

        switch (requestCode) {
        case ACTION_REQUEST_GALLERY:


            Uri selectedImageUri = data.getData();

            try {
                Uri selectedImage = selectedImageUri;
                //getContentResolver().notifyChange(selectedImage, null);
                ContentResolver cr = getContentResolver();

                Bitmap bitmap;
                bitmap = android.provider.MediaStore.Images.Media
                        .getBitmap(cr, selectedImage);
                cap.setImageBitmap(bitmap);

            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                Log.e("Camera", e.toString());
            }


            break;

        case ACTION_REQUEST_CAMERA:

            Bitmap photo = (Bitmap) data.getExtras().get("data");

            cap.setImageBitmap(photo);

            // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            Uri tempUri = getImageUri(getApplicationContext(), photo);

            // CALL THIS METHOD TO GET THE ACTUAL PATH
            File finalFile = new File(getRealPathFromURI(tempUri));

            Toast.makeText(getApplicationContext(),
                    finalFile.getAbsolutePath(), 10000).show();

            break;
        }

    }
}

private String getPath(Uri selectedImageUri) {


    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);

}

private String getRealPathFromURI(Uri tempUri) {

    Cursor cursor = getContentResolver().query(tempUri, null, null, null,
            null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);

}

private Uri getImageUri(Context applicationContext, Bitmap photo) {

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(
            applicationContext.getContentResolver(), photo, "Title", null);
    return Uri.parse(path);
}
ashatte
  • 5,294
  • 8
  • 38
  • 49
Mohamed Hemdan
  • 307
  • 3
  • 15

0 Answers0