2016-01-25 25 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