Android - Why my saved image is not appearing in the default gallery of my phone?

前端 未结 5 944
时光说笑
时光说笑 2021-01-14 01:44

I am trying to save an image from my application to the default gallery of my phone. The code below works perfectly if I have a SD card on the phone. The image saved appears

相关标签:
5条回答
  • 2021-01-14 02:07

    Try this.

    Write down this line once image stored in gallery.

    File file = ..... // Save file
    
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
    
    0 讨论(0)
  • 2021-01-14 02:11

    I haven't tried this, but I believe you need to run the Media Scanner to scan the internal storage directory so that the gallery can see your newly saved image. Check this post here.

    0 讨论(0)
  • 2021-01-14 02:13

    Copy Past this Function in your Activity

    private void scanner(String path) {
    
            MediaScannerConnection.scanFile(FrameActivity.this,
                    new String[] { path }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
    
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("TAG", "Finished scanning " + path);
                        }
                    });
        }
    

    And then add this Line where you save your image

    scanner(imageFile.getAbsolutePath());
    
    0 讨论(0)
  • 2021-01-14 02:18
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    
                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                            Uri.parse("file://"
                                    + Environment.getExternalStorageDirectory())));
                } else {
                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                            Uri.parse("file://"
                                    + Environment.getExternalStorageDirectory())));
                }
    
    0 讨论(0)
  • 2021-01-14 02:28

    Another easy way to do it. Add this after saving your file.

    File imageFile = ...
    MediaScannerConnection.scanFile(this, new String[] { imageFile.getPath() }, new String[] { "image/jpeg" }, null);
    
    0 讨论(0)
提交回复
热议问题