2017-02-26 61 views
1

画廊上下载的图片我用这种方法从wallpaperURLStr下载图片:保存在android系统

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); 
      } 
     } 

而且这种方法将它们保存在内部目录中:

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; 
    } 
} 

我可以下载图像,但我无法将其保存在画廊中。

这段代码有什么问题?

在此先感谢。

回答

1

需要广播给MediaScanner

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

退房this Question

+0

我在哪里必须插入这行代码? – OiRc

+0

保存完成后,它表示您在sdcard中有文件路径 –