Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult not called after taking picture

How know that subject is well documented and I have read a lot on that issue, but I still have the following problem: when I take a picture with my app and click on "validate" button, nothing occur. The aime of what I am doing: passing to onActivityReult function not only the thumbnail, but the "whole" picture taken by the camera.

Here is the listener as defined for the "take a picture" button:

@Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CameraTest");
            mediaStorageDir.mkdir(); // make sure you got this folder
            Log.i("Report",mediaStorageDir.toString());
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");

            try
            {
                //create directories and the file
                mediaFile.getParentFile().mkdirs();
                mediaFile.createNewFile();
            } catch (IOException e) { 
                Log.e("Report", "create error for file "+mediaFile);
                e.printStackTrace();
            }
            mFileUri = Uri.fromFile(mediaFile);
            Log.i("Report","Uri: "+mFileUri);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);// this line causes issue - onActivityResult not called...
            startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});

and here is the onActivityResult method... that is never called (and that is not declared in the onClickListener method):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("Report", "1");
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            try {
                String[] projection = {
                        MediaStore.Images.Thumbnails._ID, // The columns we want
                        MediaStore.Images.Thumbnails.IMAGE_ID,
                        MediaStore.Images.Thumbnails.KIND,
                        MediaStore.Images.Thumbnails.DATA };
                String selection = MediaStore.Images.Thumbnails.KIND + "=" + 
                        MediaStore.Images.Thumbnails.MINI_KIND;
                String sort = MediaStore.Images.Thumbnails._ID + " DESC";
                Cursor myCursor = this.managedQuery(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                        projection, selection, null, sort);
                Log.d("Report", "3");
                long imageId = 0l;
                long thumbnailImageId = 0l;
                String thumbnailPath = "";

                try {
                    myCursor.moveToFirst();
                    imageId = myCursor
                            .getLong(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
                    thumbnailImageId = myCursor
                            .getLong(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
                    thumbnailPath = myCursor
                            .getString(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
                } finally {
                    myCursor.close();
                }

                String[] largeFileProjection = {
                        MediaStore.Images.ImageColumns._ID,
                        MediaStore.Images.ImageColumns.DATA };

                String largeFileSort = MediaStore.Images.ImageColumns._ID
                        + " DESC";
                myCursor = this.managedQuery(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        largeFileProjection, null, null, largeFileSort);
                String largeImagePath = "";

                try {
                    myCursor.moveToFirst();

                    // This will actually give yo uthe file path location of the
                    // image.
                    largeImagePath = myCursor
                            .getString(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
                    mImageCaptureUri = Uri.fromFile(new File(
                            largeImagePath));

                } finally {
                    // myCursor.close();
                }
                Uri uriLargeImage = Uri.withAppendedPath(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        String.valueOf(imageId));
                Uri uriThumbnailImage = Uri.withAppendedPath(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                        String.valueOf(thumbnailImageId));

                Bitmap thumbnail = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uriThumbnailImage);
                Bitmap image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uriLargeImage);

But, as said in the title, onActivityResult is not called. Could you please find out why? Because I have tried almost everything I have found on that subject but I should have missed something.

Thanks !

like image 726
Derbie Avatar asked Dec 03 '25 08:12

Derbie


1 Answers

check if you have declared the right permissions in the AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

and be sure the file you want to write into exists:
add this below puc_img = new File(photo,"Puc_Img.jpg");

try
{
    //create directories and the file
    puc_file.getParentFile().mkdirs();
    puc_file.createNewFile();
} catch (IOException e) { }    
like image 109
bricklore Avatar answered Dec 05 '25 23:12

bricklore