2

I am using the following code to crop an image using the android crop intent.

             Intent cropIntent = new Intent("com.android.camera.action.CROP", null)
         .setDataAndType(picUri,"image/*")
         .putExtra("crop", "true")
         .putExtra("aspectX", 1)
         .putExtra("aspectY", 1)
         .putExtra("outputX", 128)
         .putExtra("outputY", 128)
         .putExtra("scale", true)
         .putExtra("return-data", false)
         .putExtra("scaleUpIfNeeded", true)
         .putExtra(MediaStore.EXTRA_OUTPUT, picUri)
         .putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
         startActivityForResult(cropIntent, PIC_CROP);

Then I get the cropped picture from this code and I want to save it in a separate folder(created by my app) in the gallery.

Here's the code to save the image :

    public static String SaveImage(Bitmap finalBitmap)
{
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Shopic Snaps");

    if(!myDir.exists())
        myDir.mkdirs();

    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image_"+ n+ GenerateRandomName() +".jpg";
    File file = new File (myDir, fname);

    if (file.exists ()) 
        file.delete ();

    try
    {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();
    }
    catch (Exception e)
    {
           e.printStackTrace();
    }

    return root + "/App Snaps/"+fname;
}

Now this function should save the picture in the gallery and return me the path to the picture.The code runs normally with no errors.

But after performing the tasks when I check the gallery, it is empty. I have no idea why it is not saving the picture in the gallery.

Edit:

I tested the app in another mobile and the pictures are saving fine in them and being showed. So I guess this is a problem in my mobile but I don't know what is causing the problem.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mj1992
  • 3,404
  • 13
  • 63
  • 102
  • Is the file stored in the desired directory and is it valid? If so, read http://stackoverflow.com/questions/13270789/how-to-run-media-scanner-in-android – Michael Butscher Jul 12 '13 at 17:47
  • file is not getting stored there , but sometimes it does, I don't know when it does and when it does not – Mj1992 Jul 12 '13 at 18:09
  • You know that in your code the returned directory is different from the directory where you store the files ("Shopic Snaps" <-> "App Snaps")? – Michael Butscher Jul 12 '13 at 18:21
  • 2
    Android does not have a `CROP` `Intent`: http://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html – CommonsWare Jul 12 '13 at 18:31
  • sory for the late reply , oh actually I changed the path accidentally. SOrry for that , by the way its ok in code and the picture is not showing in the gallery. The problem is that it is not saving the picture in the phone gallery although the code works without errors – Mj1992 Jul 12 '13 at 19:01
  • Put a breakpoint on the return statement, and check what it composes as the path. Then try to save a blank file to that directory. If it does, then the path is valid. You can say string s = ""; setText(s); And then ask why is the display empty. If it compiles, that means you have to trace it line by line. – Alexey Jul 12 '13 at 21:02
  • @CommonsWare used the library `pickNCrop` from your blog post. Thnx – Mj1992 Jul 14 '13 at 20:59

2 Answers2

0
// Initialize intent
Intent intent = new Intent("com.android.camera.action.CROP");
// set data type to be sent
intent.setType("image/*");

// get croppers available in the phone
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
// handle the case if there's no cropper in the phone
if (size == 0) {
    Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
    return;
} else {

// now this is the case if cropper exists
// initialize the Uri for the captures or gallery image

    Uri imageUri = Uri.fromFile(new File(mCurrentPhotoPath));

    // Send the Uri path to the cropper intent
    intent.setData(imageUri); 
    intent.putExtra("outputX", 200);
    intent.putExtra("outputY", 200);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);         
    intent.putExtra("scale", true);
    // Here's my attempt to ask the intent to save output data as file
    File f = null;
    // Here I initialize empty file 
    try{
            // This returns the file created
        f = setUpCroppedFile();
        mCurrentThumbPath = f.getAbsolutePath();            
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));            
        intent.putExtra("output", Uri.fromFile(f));         
    }
    catch(IOException e){
        e.printStackTrace();
    }

    intent.putExtra("return-data", false);
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------

    // If there's only 1 Cropper in the phone (e.g. Gallery )
    if (size == 1) {
            // get the cropper intent found
            Intent i        = new Intent(intent);
            ResolveInfo res = list.get(0);

            i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

            startActivityForResult(i, CROPPED_PHOTO);
    }
Muhammad Usman Ghani
  • 1,279
  • 13
  • 19
0

check the file explorer , whether the file gets stored in the device.. if the file stored but not displayed in the gallery..? try this Note: Follow the steps for both Internal and external SD Card

Go to File manager -> Android -> Data -> com.android.gallery3d. Delete the folder (com.android.gallery3d) in both internal and external SD card. Go to Settings -> Apps / Application manager -> search for Gallery -> open Gallery and tap on Clear Data . Switch off your phone and wait for few minutes (say 2-3 min) and then switch on and wait for few minutes. That's it now. All the images and videos would be showing under Gallery.