5

撮影した写真を MediaStore に追加して、Gallery アプリが (デバイスを再起動せずに) 検索できるようにします。アプリの最小 SDK は 9 です。ヘルプ、ブログ、ドキュメントを歓迎します。

4

4 に答える 4

7

ほとんどのデバイスでは、しばらく待つだけで、新しい写真が自動的に検出されます。

ギャラリーをすぐに更新したい場合は、MediaScanner クラスを使用する必要があります。これにより、ギャラリーが更新されます - 削除された写真を削除したり、新しい写真を追加したりします...

public void refreshGallery() {
    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    String newPhotoPath = "file:" + image.getAbsolutePath(); // image is the created file image
    File file = new File(newPhotoPath);
    Uri contentUri = Uri.fromFile(file);
    scanIntent.setData(contentUri);
    sendBroadcast(scanIntent);
}

これが役に立ったことを願っています!

于 2014-02-17T07:17:15.257 に答える
1
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

「保存」コードの後に​​このコード行を挿入します。

これにより、メディア スキャンがトリガーされ、すべてのフォルダー内のすべてのメディア ファイル (「.nomedia」ファイルを除く) が更新され、ギャラリーに表示されます。

ソース

MediaScanner ドキュメント

また

// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
        new String[] { file.toString() }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
    public void onScanCompleted(String path, Uri uri) {
        Log.i("ExternalStorage", "Scanned " + path + ":");
        Log.i("ExternalStorage", "-> uri=" + uri);
    }
});

Google のサンプル コード

于 2014-02-17T07:17:15.633 に答える
0

それは私のコードです

 getallimages(Environment.getExternalStorageDirectory());

そして私の機能は以下です

private void getallimages(File dir)
    {

                String[] STAR = { "*" };

        final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
        Cursor imagecursor = cntx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy);
        int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        int count = imagecursor.getCount();
        for (int i = 0; i < count; i++) {
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
            ImageItem imageItem = new ImageItem();

            if(new File(imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA))).length()<=10485760)
            {
                imageItem.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA));

                    imageItem.id = id;
                    imageItem.selection = false; //newly added item will be selected by default
                    controller.images.add(imageItem);   

            }
 }

}
于 2014-02-17T07:16:42.103 に答える
0

MediaScanner に特定のファイル、つまりイメージ ファイルをオンデマンドでスキャンするように指示できます。これにより、MediaScanner にすべてをスキャンして新しいファイルを探すよりもオーバーヘッドが少なくなります。

SO: Android でメディア スキャナーを実行する方法

于 2014-02-17T07:24:11.153 に答える