Search code examples
androidandroid-gallery

Gallery(process com.cooliris.media) has stopped unexpectedly


I am using either camera or gallery to take photos for my app.But sometimes after clicking images from camera and then switching to gallery ,gallery crashes.

code is given below:

case R.id.etUploadImage:
        Log.d(TAG, " add photo");
        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(getApplicationContext(), "sdcard not mounted",
                    Toast.LENGTH_SHORT).show();
            break;
        }
        AlertDialog.Builder photoDialog = new AlertDialog.Builder(this);
        photoDialog
                .setTitle("Photo source")
                .setCancelable(true)

                .setPositiveButton("Open Gallery",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                startActivityForResult(
                                        new Intent(
                                                Intent.ACTION_PICK,
                                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
                                        54);
                            }
                        })

                .setNegativeButton("Open Camera",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int id) {
                                //String fileName = "temp.jpg";
                                Date date = new Date();
                                DateFormat df = new SimpleDateFormat("-mm-ss");

                                String newPicFile = "Bild"+ df.format(date) + ".jpg";
                                String outPath = "/sdcard/" + newPicFile;
                                File outFile = new File(outPath);

                                ContentValues values = new ContentValues();
                                values.put(MediaStore.Images.Media.TITLE,
                                        outFile.getAbsolutePath());
                                mCapturedImageURI = getContentResolver()
                                        .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                                values);

                                Intent intent = new Intent(
                                        MediaStore.ACTION_IMAGE_CAPTURE);
                                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                        mCapturedImageURI);

                                startActivityForResult(intent, 96);

                            }
                        });
        photoDialog.show();
        break;
    }

and onActivityResult() is:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 54 && resultCode == RESULT_OK) {
        if(data !=null){
        fileName = getRealPathFromURI(data.getData());
        mUploadImage.setText(getStringNameFromRealPath((String) fileName));
        } else {
            Toast.makeText(getApplicationContext(),"Please select another image.", Toast.LENGTH_SHORT).show();
        }
    } else if (requestCode == 96 && resultCode != 0) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
                null, null);
        int column_index_data = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        fileName = cursor.getString(column_index_data);
        String s[] = fileName.split("/");
        mUploadImage.setText(s[s.length - 1]);
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCapturedImageURI));
    }
}

What is making gallery to crash.Please help me.


Solution

  • So ,After much juggling I found this solution that worked for me..

    To open Gallery or Camera :-

    if (!Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                Toast.makeText(getApplicationContext(), "sdcard not mounted",
                        Toast.LENGTH_SHORT).show();
                break;
            }
            AlertDialog.Builder photoDialog = new AlertDialog.Builder(this);
            photoDialog
                    .setTitle("Photo source")
                    .setCancelable(true)
    
                    .setPositiveButton("Open Gallery",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    startActivityForResult(
                                            new Intent(
                                                    Intent.ACTION_PICK,
                                                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
                                            54);
                                }
                            })
    
                    .setNegativeButton("Open Camera",
                            new DialogInterface.OnClickListener() {
    
                                public void onClick(DialogInterface dialog,
                                        int id) {
    
    
                                    int apiLevel = Integer.parseInt(Build.VERSION.SDK);
                                    if(apiLevel < 11) {
                                    try{
                                    //String fileName = "temp.jpg";
    
                                    Calendar c = Calendar.getInstance();
                                    CAPTURE_TITLE = "si_"+c.getTimeInMillis()+".jpg";
                                    File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", CAPTURE_TITLE);
                                    Uri ImageURI = Uri.fromFile(file);
    
    
                                    Intent intent = new Intent(
                                            MediaStore.ACTION_IMAGE_CAPTURE);
                                    intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                            ImageURI    );
    
                                    startActivityForResult(intent, 99);
    
                                    }
                                    catch(Exception e)
                                    {
                                        LogCreator lg = new LogCreator();
                                        lg.writeBarcodeToFile("Image exc1", e.toString());
                                    }
    
                                    }
                                    else{
    
                                        //String fileName = "temp.jpg";
                                        Date date = new Date();
                                        DateFormat df = new SimpleDateFormat("-mm-ss");
    
                                        String newPicFile = "Bild"+ df.format(date) + ".jpg";
                                        String outPath = "/sdcard/" + newPicFile;
                                        File outFile = new File(outPath);
    
                                        ContentValues values = new ContentValues();
                                        values.put(MediaStore.Images.Media.TITLE,
                                                outFile.getAbsolutePath());
                                        mCapturedImageURI = getContentResolver()
                                                .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                                        values);
    
                                        Intent intent = new Intent(
                                                MediaStore.ACTION_IMAGE_CAPTURE);
                                        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                                mCapturedImageURI);
    
                                        startActivityForResult(intent, 96);
                                    }
                                }
    
    
                            });
            photoDialog.show();
    

    and now the onActivityResult()

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 54 && resultCode == RESULT_OK) {
            if(data !=null){
            fileName = getRealPathFromURI(data.getData());
            mUploadImage.setText(getStringNameFromRealPath((String) fileName));
            } else {
                Toast.makeText(getApplicationContext(),"Please select another image.", Toast.LENGTH_SHORT).show();
            }
        } 
        else if (requestCode == 99 && resultCode != 0) {
            try {
                File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", CAPTURE_TITLE);
                fileName = Environment.getExternalStorageDirectory() + "/DCIM/Camera/"+CAPTURE_TITLE;
    
                Uri imgUri = Uri.fromFile(file);
                //new GetThumbnail().execute(imgUri);
                mUploadImage.setText(CAPTURE_TITLE);
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                        Uri.parse("file://"
                                + Environment.getExternalStorageDirectory())));
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
            else if (requestCode == 96 && resultCode != 0) {
                String[] projection = { MediaStore.Images.Media.DATA };
                Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
                        null, null);
                int column_index_data = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                fileName = cursor.getString(column_index_data);
                String s[] = fileName.split("/");
                mUploadImage.setText(s[s.length - 1]);
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCapturedImageURI));
            }
    
    
    
    
    
    
    }
    

    These are the methods that I used to get path :-

    /*
     * Returns image real path.
     */
    private String getRealPathFromURI(final Uri contentUri) {
        final String[] proj = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        final Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
    
        return cursor.getString(column_index);
    }
    
    /*
     * Cuts selected file name from real path to show in screen.
     */
    private String getStringNameFromRealPath(final String bucketName) {
        return bucketName.lastIndexOf('/') > 0 ? bucketName
                .substring(bucketName.lastIndexOf('/') + 1) : bucketName;
    }
    

    Hope this Help.