图库项目未更新文件下载后

时间:2016-01-25 04:45:44

标签: java android image-gallery android-gallery

我有一个壁纸应用程序,它具有保存图像功能。它工作正常但从应用程序下载的图像仅在重启移动后显示。有人可以帮我解决这个问题吗? 我下载图片的代码如下所示。

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

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

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

				        super.onPreExecute();

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

				    }

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

				        try {  

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

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

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


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


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

1 个答案:

答案 0 :(得分: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

中的