OiRc
OiRc

Reputation: 1622

Save images downloaded on gallery in Android

I'm using this method to download images from wallpaperURLStr:

  private class DownloadWallpaperTask extends AsyncTask<String, Integer, String> {
    
            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
                String wallpaperURLStr = params[0];
                String localFileName = Integer.toString(wallpaperURLStr.hashCode());
    
                try {
                    File cacheDir = GlobalClass.instance().getCacheFolder(MainActivity.this);
                    File cacheFile = new File(cacheDir, localFileName);
                    if (!cacheFile.exists()) {
                        URL wallpaperURL = new URL(wallpaperURLStr);
                        URLConnection connection = wallpaperURL.openConnection();
    
                        //get file length
                        int filesize = connection.getContentLength();
                        if (filesize < 0) {
                            downloadProgressDialog.setMax(1000000);
                        } else {
                            downloadProgressDialog.setMax(filesize);
                        }
    
                        InputStream inputStream = new BufferedInputStream(wallpaperURL.openStream(), 10240);
    
                        FileOutputStream outputStream = new FileOutputStream(cacheFile);
    
                        byte buffer[] = new byte[1024];
                        int dataSize;
                        int loadedSize = 0;
                        while ((dataSize = inputStream.read(buffer)) != -1) {
                            loadedSize += dataSize;
                            publishProgress(loadedSize);
                            outputStream.write(buffer, 0, dataSize);
                        }
    
                        outputStream.close();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                return localFileName;
            }
    
            protected void onProgressUpdate(Integer... progress) {
                downloadProgressDialog.setProgress(progress[0]);
            }
    
            protected void onPostExecute(String result) {
                downloadProgressDialog.hide();
    
                //open preview activity
                Bundle postInfo = new Bundle();
                postInfo.putString("localpath", result);
    
                if (previewPanelIntent == null) {
                    previewPanelIntent = new Intent(MainActivity.this,
                            PreviewPanel.class);
                }
    
                previewPanelIntent.putExtras(postInfo);
                startActivity(previewPanelIntent);
            }
        }

and this method to save them inside the internal directory:

public class GlobalClass {
    private static GlobalClass instance;
    private static final String applicationCacheFolder = "wallpaper_jms/cache/";
    private static final String applicationPicFolder = "wallpaper_jms/data/";
    
    public static GlobalClass instance() {
        if (instance == null) {
            instance = new GlobalClass();
        }
        return instance;
    }
    
    public File getCacheFolder(Context context) {
        File cacheDir = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            cacheDir = new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/");
            if(!cacheDir.isDirectory()) {
                cacheDir.mkdirs();
            }
        }
        
        if(!cacheDir.isDirectory()) {
            cacheDir = context.getCacheDir(); //get system cache folder
        }
        
        return cacheDir;
    }
    
    public File getDataFolder(Context context) {
        File dataDir = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            dataDir = new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/");
            if(!dataDir.isDirectory()) {
                dataDir.mkdirs();
            }
        }
        
        if(!dataDir.isDirectory()) {
            dataDir = context.getFilesDir();
        }
        
        return dataDir;
    }
}

I can download the image, but I'm not able to save it inside the gallery.

What's wrong with this code?

Upvotes: 1

Views: 80

Answers (1)

Elias Fazel
Elias Fazel

Reputation: 2113

you need to Broadcast it to MediaScanner

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

check out this Question

Upvotes: 1

Related Questions