Camera Force Closing issue in Samsung Galaxy S3 version 4.1.1

对着背影说爱祢 提交于 2019-11-28 21:58:01

I have Spent many Hours for this Single Issue and Got the Code Working in Most of the Devices With Modifying the Code as Below :

Any one having the Same issue in future can try out the Below Code.

While Calling intent for Image Capture :

String storageState = Environment.getExternalStorageState();

                    if (storageState.equals(Environment.MEDIA_MOUNTED)) {
                        Intent intent = new Intent(
                                MediaStore.ACTION_IMAGE_CAPTURE);

                        String filename = System.currentTimeMillis() + ".jpg";
                        ContentValues values = new ContentValues();
                        values.put(MediaStore.Images.Media.TITLE, filename);
                        mImageCaptureUri = getContentResolver().insert(
                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                values);

                        intent.putExtra(
                                android.provider.MediaStore.EXTRA_OUTPUT,
                                mImageCaptureUri);


                        try {

                            startActivityForResult(intent, PICK_FROM_CAMERA);
                        } catch (ActivityNotFoundException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new AlertDialog.Builder(BuildInukshk_4_Camera.this)
                                .setMessage(
                                        "External Storeage (SD Card) is required.\n\nCurrent state: "
                                                + storageState)
                                .setCancelable(true).create().show();
                    }

                } else { // pick from file
                    Intent intent = new Intent();

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

                    startActivityForResult(Intent.createChooser(intent,
                            "Complete action using"), PICK_FROM_FILE);
                }

Inside OnActivityResult Method :

case PICK_FROM_CAMERA:
                Log.i("TAG", "Inside PICK_FROM_CAMERA");

                // Final Code As Below
                try {
                    Log.i("TAG", "inside Samsung Phones");
                    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 + "=" + // Select
                                                                                    // only
                                                                                    // mini's
                            MediaStore.Images.Thumbnails.MINI_KIND;

                    String sort = MediaStore.Images.Thumbnails._ID + " DESC";

                    // At the moment, this is a bit of a hack, as I'm returning ALL
                    // images, and just taking the latest one. There is a better way
                    // to
                    // narrow this down I think with a WHERE clause which is
                    // currently
                    // the selection variable
                    Cursor myCursor = this.managedQuery(
                            MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                            projection, selection, null, sort);

                    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();
                    }

                    // Create new Cursor to obtain the file Path for the large image

                    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_samsung = Uri.fromFile(new File(
                                largeImagePath));
                        mImageCaptureUri = null;
                    } finally {
                        // myCursor.close();
                    }

                    // These are the two URI's you'll be interested in. They give
                    // you a
                    // handle to the actual images
                    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));

                    // I've left out the remaining code, as all I do is assign the
                    // URI's
                    // to my own objects anyways...
                } catch (Exception e) {
                    mImageCaptureUri_samsung = null;
                    Log.i("TAG",
                            "inside catch Samsung Phones exception " + e.toString());

                }

                try {
                    Log.i("TAG",
                            "URI Samsung:" + mImageCaptureUri_samsung.getPath());

                } catch (Exception e) {
                    Log.i("TAG", "Excfeption inside Samsung URI :" + e.toString());
                }

                try {

                    Log.i("TAG", "URI Normal:" + mImageCaptureUri.getPath());
                } catch (Exception e) {
                    Log.i("TAG", "Excfeption inside Normal URI :" + e.toString());
                }



                break;

After Running Below Code you Will get Two URIs mImageCaptureUri_samsung and mImageCaptureUri

you will get the mImageCaptureUri as your Path if you are running the App with Simple Devices and you will get your Cpatured Image path in mImageCaptureUri_samsung if you are running with Devices Like Samsung Galaxy S3.

Further you all can go ahead with your Code. it Works For me Very Fine With all the Devices i have tested on.

Also if Someone is having Problem with Above Code than they can reference the Below Great Link Solution of Samsung Galaxy S3

Hope it will Help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!