保存图像后检查SD卡(Android)

时间:2015-10-31 01:26:12

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

我的意图是,完成后,将图像保存到/ sdcard / Aperture。虽然图像确实保存,但一旦保存就不会显示图像。可能是什么问题呢?我尝试在我的On Activity Result中使用扫描广播,但这会导致崩溃,并且不会扫描图像。

@Override
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) {
        // return from file upload
        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));
            }
        }
    }
}

 final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
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 :(得分:2)

Impression在我认为的某些地方使用了MediaScannerConnection。这是使用扫描仪意图的更现代版本。它还会告诉您何时扫描完成并为您提供新URI(如果文件已从媒体数据库中删除,则为null)。

MediaScannerConnection.scanFile(this,
          new String[] { filePath }, null,
          new MediaScannerConnection.OnScanCompletedListener() {
      @Override
      public void onScanCompleted(String path, Uri uri) {
          Log.i("ExternalStorage", "Scanned " + path + ":");
          Log.i("ExternalStorage", "-> uri=" + uri);
      }
});

扫描完成后,您需要重新加载媒体和相册。