Android ACTION_IMAGE_CAPTURE with EXTRA_OUTPUT in internal memory

て烟熏妆下的殇ゞ 提交于 2019-12-04 09:15:28

The native camera app cannot save the image in your app's private internal directories as those are only available to your particular app.

Instead you can create a custom camera activity to save images to your internal directories or you need to use the stock camera app with external storage.

Note: if you plan on creating a custom camera activity make sure you target at least 2.3 and up. Anything below that mark is very difficult to work with.

The Camera activity will not be able to save the file into your activity's private files directory, that's why it fails quietly. You can move the image from the external storage into your files dir in onActivityResult.

This is possible using the file provider. Please refer sample

  public getOutputUri(@NonNull Context pContext) {
    String photo = photo.jpeg;//your file name
    File photoFile = new File(pContext.getFilesDir(), photo);
    Uri lProviderPath = FileProvider.getUriForFile(pContext,
        pContext.getApplicationContext()
            .getPackageName() + ".provider", photoFile);
    return lProviderPath;
  }



  private void capturePhoto() {
      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
      cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getOutputUri(this));
      cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      startActivityForResult(cameraIntent, 1);
  }

Refer following android document for more details https://developer.android.com/reference/android/support/v4/content/FileProvider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!