【问题标题】:Media Scanner Is Scanning, But Not Refreshing (Android)媒体扫描仪正在扫描,但不刷新 (Android)
【发布时间】:2015-10-31 17:13:50
【问题描述】:

我有一个图库应用,它也有相机功能。拍完照片后,我的 On Activity Result 需要媒体扫描仪来扫描文件。虽然它确实扫描文件,并且 LogCat 报告它的确切位置,并且它保存在指定的目录中,但我的画廊和其他人(如谷歌照片)直到下次重新启动或很长时间才显示图像通过了。我究竟做错了什么?

 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);
    }
}}

【问题讨论】:

    标签: java android android-intent camera android-camera


    【解决方案1】:

    从不硬编码路径。与辅助帐户一起使用时,您的代码对于数以亿计的 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()}
    

    【讨论】:

    • 我的相机意图也使用 /sdcard/Aperture。它保存到它,但我也应该替换它吗?
    • @VirusThePanda:是的,出于同样的原因。在 Android 4.2+ 平板电脑和 Android 5.0+ 手机上,可以有多个用户帐户。每个用户都有自己的位置来存储外部存储等内容,并且他们的路径会有所不同。
    • 好的,在我的相机意图中,我使用了:tring outPath = "/Environment.getExternalStorageDirectory()/Aperture/" + newPicFile; 这是正确的吗?还是应该和你的答案完全一样?
    • 现在根本没有创建文件。你能帮忙吗?我已将代码恢复为原始状态,以便您查看。这是相机的意图。 (查看问题。已编辑)
    • @VirusThePanda:好吧,如果你使用相同的文件名,你的运气会好一些。 new Date() 在不同的时间点会有所不同,因此基于new Date() 的文件名在不同的时间点也会有所不同。你设置了mCameraFileName,然后当你调用scanFile()时忽略它onActivityResult()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多