媒体扫描仪正在扫描,但未刷新 (Android)

Media Scanner Is Scanning, But Not Refreshing (Android)

我有一个图库应用程序,它也具有相机功能。拍照后,我的 Activity 结果要求媒体扫描仪扫描文件。虽然它确实扫描文件,并且 LogCat 报告它的确切位置,并且它保存在指定的目录中,但是我的画廊和其他人如 Google 照片直到下次重新启动,或者已经过去了很长时间。我究竟做错了什么?

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SETTINGS_REQUEST && resultCode == Activity.RESULT_OK) {
        MediaFragment content = (MediaFragment) getFragmentManager().findFragmentById(com.marlonjones.aperture.R.id.content_frame);
        if (content != null) content.reload();
        reloadNavDrawerAlbums();
    }
    if (requestCode == NEW_PICTURE) {
        if (resultCode == Activity.RESULT_OK) {

            Uri uri = null;
            if (data != null) {
                uri = data.getData();
            }
            if (uri == null && mCameraFileName != null) {
                uri = Uri.fromFile(new File(mCameraFileName));
            }
            Date date = new Date();
            DateFormat df = new SimpleDateFormat("-mm-ss");
            String newPicFile = "PH" + df.format(date) + ".jpg";
            String outPath = "/sdcard/Aperture/" + newPicFile;
            MediaScannerConnection.scanFile(this,
                    new String[]{Uri.fromFile(new File (outPath)).toString()}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });
        }
    }
}

 public void camera(MenuItem menu) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA);
        if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.CAMERA},
                    REQUEST_CODE_ASK_PERMISSIONS);
            return;
        }
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("-mm-ss");

        String newPicFile = "PH" + df.format(date) + ".jpg";
        String outPath = "/sdcard/Aperture/" + newPicFile;
        File outFile = new File(outPath);
        mCameraFileName = outFile.toString();
        Uri outuri = Uri.fromFile(outFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
        startActivityForResult(intent, NEW_PICTURE);
    }
     else {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("-mm-ss");

        String newPicFile = "PH" + df.format(date) + ".jpg";
        String outPath = "/sdcard/Aperture/" + newPicFile;
        File outFile = new File(outPath);
        mCameraFileName = outFile.toString();
        Uri outuri = Uri.fromFile(outFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
        startActivityForResult(intent, NEW_PICTURE);
    }
}}

切勿对路径进行硬编码。当与辅助帐户一起使用时,您的代码对于数以亿计的 Android 设备是不正确的。使用方法获取文件系统位置。在您的情况下,替换:

String outPath = "/sdcard/Aperture/" + newPicFile;

与:

File picFile=new File(new File(Environment.getExternalStorageDirectory(), "Aperture"), newPicFile);

其次,scanFile() 采用路径数组,而不是 Uri 值。替换:

new String[]{Uri.fromFile(new File (outPath)).toString()}

与:

new String[]{picFile.getAbsolutePath()}