Adam Katz Adam Katz - 1 month ago 8
Android Question

Download images and save them but dont want them in the gallery

I have a chat app and I need to save the images that the user sends receives. For the images the user sends I am saving it to an image folder like so

private void imageDownload(final String url){

Picasso.with(getContext())
.load(url)
.into(getTarget(url));

}

//target to save
private static Target getTarget(final String url){
Target target = new Target(){

@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {

@Override
public void run() {

File heyJudeFile = new File(Environment.getExternalStorageDirectory().getPath() + "/Hey Jude");
if (!heyJudeFile.exists()){
heyJudeFile.mkdirs();
}

localProfilePictureAdress = Environment.getExternalStorageDirectory().getPath() + "/Hey Jude" + "/" + url.substring(url.lastIndexOf("/")+1);

File file = new File(localProfilePictureAdress);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
ostream.flush();
ostream.close();
} catch (IOException e) {
Log.e("IOException", e.getLocalizedMessage());
}
}
}).start();

}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}
};
return target;
}


But for images the user receives I want to save the images but I do not want the images to be viewable in the gallery how does that work?

Thanks

Answer

Use below method to hide media from gallery

/* To Hide media file in gallery  */ 
public void createNoMedia(String myDir){

    File noMediaFile = new File(myDir, ".nomedia");
    if (!noMediaFile.exists()) {
        try { 
            noMediaFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    } 
}

/* To use this */
dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/MyImages/";
File newDir = new File(dir);
newDir.mkdirs();
createNoMedia(dir);

and in manifest.xml You also need the WRITE_EXTERNAL_STORAGE permission.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />