【问题标题】:Gallery item is not updated After file downloading图库项目未更新 文件下载后
【发布时间】:2016-01-25 04:45:43
【问题描述】:

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

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

【问题讨论】:

    标签: java android image-gallery android-gallery


    【解决方案1】:

    您需要告诉系统运行媒体扫描仪,然后文件才会出现在图库中。可以这样做:

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

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

    在你的 onPostExecute 中

    【讨论】:

      猜你喜欢
      • 2016-09-26
      • 2012-12-21
      • 1970-01-01
      • 2013-09-03
      • 2013-03-22
      • 2016-08-31
      • 2011-04-30
      • 1970-01-01
      相关资源
      最近更新 更多