how to hide images from android gallery
Asked Answered
E

5

17

One part of my current project is downloading images from URL then saving to SDCard. But the problem is all of saved images in sdcard is displayed in android gallery page. What I want is "I don't want all of my saved images in android gallery."

Eluviation answered 23/7, 2013 at 2:22 Comment(1)
Refer this link[here stackoverflow link][1] [1]: #5896492Barber
E
36
  1. Add ".nomedia" file. Did not work? Try the second option.

  2. Save your files to a folder that starts with ".", e.g., /sdcard/.myimages/

Europa answered 23/7, 2013 at 2:32 Comment(5)
Add ".nomedia" in /sdcard/myapps/ folder? And how can I get ".nomedia" file?Eluviation
Just create one, an empty file :)Europa
This doesn't work man. Not the first option, not the second option, not even creating the files with prefix "."Kurus
@Odaym: This doesn't work is a very vague claim. How exactly it is not working? What steps did you take? Did you verify that you are not seeing old cached images?Europa
Doesn't work meaning that the images still show up after having created the directory with a "." prefix, creating the ".nomedia" file inside that directory and even creating the images with the "." prefix. All after triggering a rescan of media. Saw this too code.google.com/p/android/issues/detail?id=24162Kurus
H
10

You can create .nomedia file using this code:
String NOMEDIA=".nomedia";

File Folder = new File(Environment.getExternalStorageDirectory() + "/mydir");
   if(Folder.mkdir()) { 
      nomediaFile = new File(Environment.getExternalStorageDirectory() + "/mydir/"+ NOMEDIA);
      if(!nomediaFile.exists()){
          nomediaFile.createNewFile();
      }
   }
Harassed answered 23/7, 2013 at 3:52 Comment(0)
M
1

I found a simple way(I think so).

Create a folder in 'Android' folder(where data & obb folders are present) and put your media(pics,videos, etc.,) in the folder you've created.

Gallery app ignores that 'Android' folder. Simple :-)

Manoff answered 7/6, 2015 at 20:0 Comment(0)
C
1

its so late and i know other answers are complete but i think putting .nomedia file makes all pictures in that particular folder hidden :

just change extension of image file programmatically and this way gallery cant detect it as an image . eg if your image is "pic1.jpg" rename it to "pic1.aaa"; and when you want show it again rename it again

Colloidal answered 31/3, 2016 at 13:22 Comment(1)
its perfect solution!Housebreak
A
0

I have Used Three Approaches:

  1. folder Name with dot .TafheemReels
  2. image Extension : .falseExtension

file Path Looks like :

/storage/emulated/0/Download/.TafheemReels/mypicture.falseExtension
  1. show the image with picasso.

The saving method :

 public static void saveImageToDownloadStorage(Context mContext, Bitmap bitmap, String fileName) {


        class saveThread extends  Thread {

            @Override
            public void run() {
                super.run();


                FileOutputStream outStream = null;

                File outFile;
                try {
                    outFile = reelImagePath ( fileName);
                    outStream = new FileOutputStream(outFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                    outStream.flush();
                    outStream.close();

                    MediaScannerConnection.scanFile(mContext, new String[]{outFile.getPath()}, new String[]{"image/jpeg"}, null);

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }

        saveThread thread = new saveThread();
        thread.setDaemon(true);
        thread.start();


    }


public static File reelImagePath (String fileName){
        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/.TafheemReels";
        File dir = new File(root);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        String filename = String.format("%s.falseExtension", fileName);
        File  outFile = new File(dir, filename);

        return outFile ;

    }

And the image showing with picasso :

File localPath = reelImagePath("mypicture");
 Uri uri = Uri.fromFile(localPath);
    
            Picasso.with(mContext).cancelRequest(imageView);
    
            Picasso.with(mContext).load(uri).into(imageView);
    
       
    

In this way, I found that Picasso do not look after the file extension.

And your Image file is hidden forever from the gallery app.

Annabell answered 19/8, 2023 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.