使用MediaScannerConnection scanFile扫描下载的图像文件

3

我正在开发一款应用程序,其中我将图像保存到一个目录中,但在重新启动手机之前,这些图像不会在相册中显示。

以下是我的代码片段:

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() + "/mydownloaddir/");
                    dir.mkdirs();
                    String fileName = idStr;
                    file = new File(dir, fileName);
                    FileOutputStream fos = new FileOutputStream(file);
                    bmImg.compress(CompressFormat.JPEG, 75, fos);   
                    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 to Folder 'mydownloaddir'", Toast.LENGTH_SHORT).show();
                pDialog.dismiss();
            }
        }

接下来我应该使用哪些代码,才能让图像在画廊中使用medisscanner显示出来?

我在这里得到了类似以下的东西,但是无法正确地使用它:

MediaScannerConnection.scanFile(ApplicationContext.context, new String[] { imageFile.getPath() }, null,
          new MediaScannerConnection.OnScanCompletedListener() {
            @Override
            public void onScanCompleted(String path, Uri uri) {
              Log.i(TAG, "Scanned " + path);
            }
          });

请帮忙

1个回答

1
使用

标签。

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

第一段://但它只适用于API&lt;19
第二段:

第二种方法

private void scanFile(String path) {

        MediaScannerConnection.scanFile(MainActivity.this,
                new String[] { path }, null,
                new MediaScannerConnection.OnScanCompletedListener() {

                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("TAG", "Finished scanning " + path);
                    }
                });
    }

调用它的方式如下:
scanFile(yourFile.getAbsolutePath());

同时请参阅此答案


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接