1

I just inserted some pictures in my internal storage but they aren't visible in the gallery then.

Can someone explain me why?

Here is my code :


    File photosFolder = new File(getContext().getExternalFilesDir(null),"myPictures");
            photosFolder.mkdirs();
            String[] pictures = null;
            try {
                pictures = assetManager.list("photos");
            } catch (IOException e) {
                Log.e("tag", "Failed to get asset file list.", e);
            }
            for(String filename : pictures) {
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = assetManager.open("photos/"+filename);
                    File outFile = new File(photosFolder, filename);
                    out = new FileOutputStream(outFile);
                    copyFile(in, out);
                    in.close();
                    in = null;
                    out.flush();
                    out.close();
                    out = null;
                    MediaScannerConnection.scanFile(getContext(), new String[]{outFile.toString()}, null, new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("External Storage", "Scanned"+ path +":");
                            Log.i("External Storage", "uri "+uri);
                        }
                    });
                } catch(IOException e) {
                    Log.e("tag", "Failed to copy asset file: " + filename, e);
                }
            }

I don't have any result with this, any help ?

Loulou ChabChab
  • 71
  • 1
  • 10
  • On Android 10+, other apps have no rights to access files in your chosen location. On earlier versions of Android, eventually the `MediaStore` will index them, though you can accelerate the process [by using `MediaScannerConnection`](https://stackoverflow.com/q/32789157/115145). – CommonsWare Oct 21 '21 at 14:01
  • @CommonsWare I'm still a beginner, and I don't really understand how to implement it with the MediaScanner :( – Loulou ChabChab Oct 21 '21 at 14:08
  • Um, well, [the linked-to question and answer](https://stackoverflow.com/q/32789157/115145) has sample code in both Java and Kotlin. – CommonsWare Oct 21 '21 at 14:09
  • Are they visible in your directory? – Danish Oct 21 '21 at 14:13
  • @Danish yes they are – Loulou ChabChab Oct 21 '21 at 14:18
  • @CommonsWare `MediaScannerConnection.scanFile(getContext(),pictures, new String[] {"photos/jpg"}, null);` I tried this but it's still not working :-( – Loulou ChabChab Oct 21 '21 at 14:40

1 Answers1

1

Give this a try:

File photosFolder = null;
if(Build.VERSION_CODES.R>Build.VERSION.SDK_INT) {
    photosFolder = new File(Environment.getExternalStorageDirectory(), "myPictures");//this will create a seperate directory in your external storage if you are using android 10 device
}
else
{
    photosFolder=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath(),"myPictures");//though depreciated it still works.In android 11 you can create your directory inside public directories using this method.
}
if (!file.exists()) {
    file.mkdir();
}

String[] pictures = null;

try {
    pictures = assetManager.list("photos");
} catch (IOException e) {
    Log.e("tag", "Failed to get asset file list.", e);
}
for(String filename : pictures) {
    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open("photos/"+filename);
        File outFile = new File(photosFolder, filename);
        out = new FileOutputStream(outFile);
        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
        MediaScannerConnection.scanFile(this,
                new String[] { outFile.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    } catch(IOException e) {
                        Log.e("tag", "Failed to copy asset file: " + filename, e);
                    }
                }
    }

You can check the documentation here.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Danish
  • 676
  • 5
  • 10
  • I tried this, it returned that on logs : **I/External Storage: Scanned/storage/emulated/0/Android/data/com.data.myApp/files/myPictures/dataset12soleil.jpg: I/External Storage: uri null** Should I need "Permisssions :READ EXTERNAL STORAGE" or just "WRITE EXTERNAL STORAGE" is enough ? – Loulou ChabChab Oct 22 '21 at 07:20
  • You have to add both the permission. – Danish Oct 22 '21 at 14:06
  • I have both of them :( @Danish – Loulou ChabChab Oct 22 '21 at 14:07
  • Actually from android 10 onwards you wont see those picture in your gallery that are present in Android/data/yourpackagename. – Danish Oct 22 '21 at 14:19
  • You can check my updated answer. – Danish Oct 22 '21 at 14:26
  • **E/tag: Failed to copy asset file: dataset12soleil.jpg java.io.FileNotFoundException: /storage/emulated/0/myPictures/dataset12soleil.jpg: open failed: ENOENT (No such file or directory)** I have this error on this line : `out = new FileOutputStream(outFile);` and "getExternalStoragePublicDirectory" is crossed for this line `photosFolder=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath(),"myPictures");` – Loulou ChabChab Oct 26 '21 at 08:52
  • 1
    well finally it's working ! I had to rename my "myPictures" folder to "Pictures". Then I had a permission issue and this post helped me resolve the issue: [https://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android](https://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android) – Loulou ChabChab Oct 26 '21 at 12:41
  • Hi, @Danish. I would like to give an suggestion. Next time onwards, pls add the link in the correct format. Adding it seperately does not make any sense and does not look nicely formatted – Sambhav Khandelwal Jan 28 '22 at 16:39