Use MediaScannerConnection scanFile To Scan Downloaded Image File

Deadly 提交于 2019-12-11 09:53:09

问题


I am working on an app in which I save image(s) to a directory but the Images wont show up in gallery until I restart the phone.

Here's My Code Snippet

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

What code should I use next to make the Images showup in gallery using medisscanner

I got something like this on here But unable to use it properly:

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

Please Help


回答1:


Use

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

//but it only works on API <19

Second Method

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

Call it like

scanFile(yourFile.getAbsolutePath());

Also see this Answer



来源:https://stackoverflow.com/questions/26912832/use-mediascannerconnection-scanfile-to-scan-downloaded-image-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!