Adam Katz Adam Katz - 10 months ago 34
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){



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

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

public void run() {

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

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

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


public void onBitmapFailed(Drawable errorDrawable) {


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?


Answer Source

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 { 
        } catch (IOException e) {

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

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

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