2017-02-26 36 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中有文件路徑 –