2016-01-25 24 views
0

我有一個壁紙應用程序具有保存圖像功能。它的工作正常,但從應用程序下載的圖像只顯示重新啓動移動後。有人可以幫我解決這個問題嗎? 我的下載圖片的代碼如下所示。圖庫項目未更新文件下載後

public class SaveTask extends AsyncTask<String , String , String> 
 
\t \t \t { 
 
\t \t \t \t 
 
\t \t \t \t  private Context context; 
 
\t \t \t \t  private ProgressDialog pDialog; 
 
\t \t \t \t  String image_url; 
 
\t \t \t \t  URL myFileUrl; 
 
\t \t \t \t  String myFileUrl1; 
 
\t \t \t \t  Bitmap bmImg = null; 
 
\t \t \t \t  File file ; 
 

 
\t \t \t \t  public SaveTask(Context context) { 
 
\t \t \t \t   this.context = context; 
 
\t \t \t \t  } 
 

 
\t \t \t \t  @Override 
 
\t \t \t \t  protected void onPreExecute() { 
 
\t \t \t \t   // TODO Auto-generated method stub 
 

 
\t \t \t \t   super.onPreExecute(); 
 

 
\t \t \t \t   pDialog = new ProgressDialog(context); 
 
\t \t \t \t   pDialog.setMessage("Downloading Image ..."); 
 
\t \t \t \t   pDialog.setIndeterminate(false); 
 
\t \t \t \t   pDialog.setCancelable(false); 
 
\t \t \t \t   pDialog.show(); 
 

 
\t \t \t \t  } 
 

 
\t \t \t \t  @Override 
 
\t \t \t \t  protected String doInBackground(String... args) { 
 
\t \t \t \t   // TODO Auto-generated method stub 
 

 
\t \t \t \t   try { 
 

 
\t \t \t \t    myFileUrl = new URL(args[0]); 
 
\t \t \t \t    //myFileUrl1 = args[0]; 
 

 
\t \t \t \t    HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); 
 
\t \t \t \t    conn.setDoInput(true); 
 
\t \t \t \t    conn.connect();  
 
\t \t \t \t    InputStream is = conn.getInputStream(); 
 
\t \t \t \t    bmImg = BitmapFactory.decodeStream(is); 
 
\t \t \t \t   } 
 
\t \t \t \t   catch (IOException e) 
 
\t \t \t \t   {  
 
\t \t \t \t    e.printStackTrace(); 
 
\t \t \t \t   } 
 
\t \t \t \t   try {  
 

 
\t \t \t \t    String path = myFileUrl.getPath(); 
 
\t \t \t \t    String idStr = path.substring(path.lastIndexOf('/') + 1); 
 
\t \t \t \t    File filepath = Environment.getExternalStorageDirectory(); 
 
\t \t \t \t    File dir = new File (filepath.getAbsolutePath() + "/Hindi Picture/"); 
 
\t \t \t \t    dir.mkdirs(); 
 
\t \t \t \t    String fileName = idStr; 
 
\t \t \t \t    file = new File(dir, fileName); 
 
\t \t \t \t    FileOutputStream fos = new FileOutputStream(file); 
 
\t \t \t \t    bmImg.compress(CompressFormat.JPEG, 75, fos); 
 
\t \t \t \t    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(idStr)))); 
 
\t \t \t \t    fos.flush();  
 
\t \t \t \t    fos.close(); 
 
\t \t \t \t    
 

 

 
\t \t \t \t   } 
 
\t \t \t \t   catch (Exception e) 
 
\t \t \t \t     { 
 
\t \t \t \t      e.printStackTrace(); 
 
\t \t \t \t     } 
 
\t \t \t \t   return null; 
 
\t \t \t \t  } 
 

 

 
\t \t \t  @Override 
 
\t \t \t  protected void onPostExecute(String args) { 
 
\t \t \t   // TODO Auto-generated method stub 
 
\t \t \t  \t Toast.makeText(SlideImageActivity.this, "Image Saved Succesfully Hindi Picture Folder/", Toast.LENGTH_SHORT).show(); 
 
\t \t \t  \t if (mInterstitial.isLoaded()) { 
 
\t \t \t \t \t \t mInterstitial.show(); 
 
\t \t \t \t \t } 
 
\t \t \t   pDialog.dismiss(); 
 
\t \t \t  } 
 
\t \t \t }

回答

0

你需要告訴系統運行媒體掃描儀和只有那麼該文件將出現在畫廊。這是可以做到如下:

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

OR

MediaScannerConnection.scanFile(this, 
      new String[] { filepath.getAbsolutePath() }, null, 
      new MediaScannerConnection.OnScanCompletedListener() { 
     public void onScanCompleted(String path, Uri uri) { 
     // your code here 
     } 
}); 

在onPostExecute