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

时间:2015-10-31 17:13:50

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

我有一个同时具有相机功能的图库应用。拍摄照片后,我的On Activity Result要求媒体扫描程序扫描文件。虽然它确实扫描了文件,并且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);
    }
}}

1 个答案:

答案 0 :(得分:3)

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