19

したがって、私のアプリケーションでは、ある時点で一連の画像を一時フォルダーに保存し、ギャラリーにすぐに表示したいと考えています。再起動後はそうしますが、それ以外の場合はそうしません。

sendBroadcast メソッドを使用してみました:

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

しかし、許可エラーが発生します:

E/AndroidRuntime( 2628): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=2628, uid=10068

AndroidManifest にアクセス許可がありませんか、それともサポートされていないだけですか? ありがとう

4

8 に答える 8

9

を試しEnvironment.getExternalStorageDirectory().toString()ましたが、カスタム フォルダが見つかりませんでした。

この問題を解決するための私の経験を共有したかっただけです。

最後に、一度にMediaScannerConnection1 つのファイルをスキャンするように設定したところ、それらの画像を保持していたフォルダーが見つからないことが表示されました。各画像をダウンロードするたびMediaScannerConnectionに、フォルダー自体ではなく Uri としてファイル パスを呼び出しました。

また、多くの人がsendBroadcastkitkat での作業をやめた理由を尋ねましたが、その答えは、sendBroadcast悪用され、何度も呼び出され、システムのバッテリーを消耗させたり速度を低下させたりしたため、悪用を防ぐために直接呼び出しを削除したことは理にかなっています。すべての画像を保持するフォルダーに対して上記のソリューションを使用することを望んでいましたが、フォルダーでは機能しませんでした。フォルダーを見つけるとカスタムフォルダー内の残りのファイルが公開されることを望んでいましたが、私の場合はそうではありませんでした。将来的にはより良い答えが見つかることを願っています...

これが私のコードのスニペットです。

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);
                }
              });
于 2014-01-24T02:24:05.960 に答える
7

これを試してください:ファイルをフォルダーに保存した後、以下のコードを記述します

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your image path"))));
于 2016-09-12T10:59:42.530 に答える
5

以下のコードは、画像を保存した後にギャラリーを更新するためにすべてのデバイスを機能させます。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
       Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
       File f1 = new File("file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
       Uri contentUri = Uri.fromFile(f1);
       mediaScanIntent.setData(contentUri);
       sendBroadcast(mediaScanIntent);
   } else {
       sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
   }
   sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your image path"))));
于 2016-08-31T05:36:13.350 に答える
2

I know its late but try this can working in all versions:

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    Intent mediaScanIntent = new Intent(
                            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    Uri contentUri = Uri.fromFile(out); //out is your file you saved/deleted/moved/copied
                    mediaScanIntent.setData(contentUri);
                    this.sendBroadcast(mediaScanIntent);
                } else {
                    sendBroadcast(new Intent(
                            Intent.ACTION_MEDIA_MOUNTED,
                            Uri.parse("file://"
                                    + Environment.getExternalStorageDirectory())));
                }
于 2015-02-11T07:21:09.617 に答える
0

SendBroadcast の代わりに MediaScannerConnection を使用します。

MediaScannerConnection.MediaScannerConnectionClient(
{
    @Override
    public void onScanCompleted(String path, Uri uri) {

        if (path.equals(**your filename**.getAbsolutePath()))
        {
            Log.i("Scan Status", "Completed");
            Log.i("uri: ",uri.toString());

            conn.disconnect();
        }
    }
    @Override
    public void onMediaScannerConnected()
    {
        // TODO Auto-generated method stub
        conn.scanFile(**your file name**.getAbsolutePath(),null);
    }
});

conn.connect();
于 2014-08-13T09:05:23.047 に答える