1

I'm trying to save my edited image into Gallery from where user can access it. I have used "Add the photo to the Gallery" from reference: https://developer.android.com/training/camera/photobasics.html

Here is my Code:

String mCurrentPhotoPath;
public void startSave() {

    FileOutputStream fileOutputStream = null;
    File new_file = null;

    try {
        new_file = createImageFile();
        fileOutputStream = new FileOutputStream(new_file);
        imageView.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        Toast.makeText(this, "save Image Success", Toast.LENGTH_SHORT).show();
        fileOutputStream.flush();
        fileOutputStream.close();
    }
    catch (FileNotFoundException e){
        e.printStackTrace();
    }
    catch (IOException e){
        e.printStackTrace();
    }

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

I have also changed AndroidManifest.xml like this and given permission to write in external storage and granted Uri permission.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mypackage.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>
</application>

I have added xml file paths according to this:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" 
path="Android/data/com.sparkexel.blurd1/files/Pictures" />
</paths>

I can't get my head around what is the problem here. First, there was an error message about external permission or URI permission not granted. I have set URI permission after that. Finally, There is no error message in console. But I can't save my image into the gallery. I have logged path of my saved file. and it is:

/storage/emulated/0/Android/data/com.mypackage/files/Pictures/JPEG_20171216_171109_2682421804312420480.jpg

From that, I assumed that file is saving successfully. But it does not show up in Gallery. I have tried many solutions but nothing works. Please help me.

tamtom
  • 2,474
  • 1
  • 20
  • 33
JaySparkexel
  • 165
  • 4
  • 16
  • did you check whether you got that permissions to write or read? – Jyoti JK Dec 16 '17 at 11:50
  • You Have to Run Media Scanner on the Output Folder. Check My Answer – Tomin B Azhakathu Dec 16 '17 at 12:00
  • I have checked permission using this code:if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { Toast.makeText(this, "Permission Denied", Toast.LENGTH_LONG).show(); }....... and i think permission is denied. I have added it in android manifest so I don't know what's wrong @Joe – JaySparkexel Dec 16 '17 at 12:14
  • The way you post your problem is pretty strange. You are trying to save a bitmap. `File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);`. And you are trying to save it to external storage as you can see. Now first tell us if the file is created or that you got io exceptions. You can check with any file explorer app on your device if the file is created. Please inform us. – greenapps Dec 16 '17 at 12:55
  • Once the file is created you can try it to get it in the mediastore. If it is in the media strore than apps like Gallery app will show your file. What is wrong with your code is that even if you have a FileNotFoundException or an IOException you still invoke the scanner. You should not do that of course. You should display a toast in those catch blocks to inform the user and then return. Please adapt your code. – greenapps Dec 16 '17 at 12:57
  • `if(ContextCompat.checkSelfPermission()`. That is not enough code. If the permission is denied as you say you have to add other code upon which the user of your app will be asked to allow the permission. It looks as if you are not doing that. – greenapps Dec 16 '17 at 13:00
  • After your suggestion @greenapps, I logged error message. and I got message permission denied. I already have provided permission in android manifest to read and write external storage. What should I do, Does I have to provide any more permissions? – JaySparkexel Dec 16 '17 at 13:46
  • Ok, I got it I have to ask for permission at a run time after Android 6. There is no problem in any other code. Thank you very much @greenapps – JaySparkexel Dec 16 '17 at 13:52

1 Answers1

3

The File Will be Displayed only if after a Successful Completion of media Scanner.

Note

  1. Save the File To the External Storage . Now the Path is Android Data Folder. Most of the time the Files inside the data Folder wont be Scanned By Media Scanner. For that use Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/Foldername/" as Folder Path

    Environment.getExternalStorageDirectory().getAbsolutePath() will Return the ExternalStorge as the Path

  2. Run Media Scanner

Also Refer Trigger Media Scanner Programattically

Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28