Force scan files after taking photo

前端 未结 4 1362
星月不相逢
星月不相逢 2020-12-03 09:34

on api level 4 (android 1.6), after taking photo using:

Intent intent = new Intent(\"android.media.action.IMAGE_CAPTURE\");
File photo = new File(Environment         


        
相关标签:
4条回答
  • 2020-12-03 09:51

    Use this code:

    public static void scanFile(Context context, String path, String mimeType ) {
        Client client = new Client(path, mimeType);
        MediaScannerConnection connection =
                new MediaScannerConnection(context, client);
        client.connection = connection;
        connection.connect();
    }
    
    private static final class Client implements MediaScannerConnectionClient {
        private final String path;
        private final String mimeType;
        MediaScannerConnection connection;
    
        public Client(String path, String mimeType) {
            this.path = path;
            this.mimeType = mimeType;
        }
    
        @Override
        public void onMediaScannerConnected() {
            connection.scanFile(path, mimeType);
        }
    
        @Override
        public void onScanCompleted(String path, Uri uri) {
            connection.disconnect();
        }
    }
    

    Then just call scanFile(imageUri.getPath(), null).

    Don't use encoded path and don't use "*/*" as a MIME type because null value makes scanner to determine MIME type automatically.

    0 讨论(0)
  • Whenever you add a file, let MediaStore Content Provider knows about it using the sendBroadcast method

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(mediaFileAdded)));
    

    For deletion, use:

    getContentResolver().delete(uriOfMediaFileDeteled, null, null)
    

    Main advantage: work with any mime type supported by MediaStore

    In your case, do this in onActivityResultMethod (i.e.) after the photo has been successfuly taken

    0 讨论(0)
  • 2020-12-03 10:02

    after taking picture try calling insert() function of ContentResolver passing the information about the picture.

    public final Uri insert (Uri url, ContentValues values)
    

    It will actually add the picture to the database and create picture's thumbnail image for you. It will also be added to the thumbnail database. Hope this helps!

    0 讨论(0)
  • 2020-12-03 10:08

    Use

    MediaStore.Images.Thumbnails.getThumbnail(ContentResolver cr, long origId, int kind, BitmapFactory.Options options)
    

    to force the creation of the thumbnail for an image.

    0 讨论(0)
提交回复
热议问题