新编辑:

我正在开发一个android应用程序,并希望支持androidQ。当我的应用程序在目标API(
拍摄第一张照片时,可以在自定义照片选择器中找到它。
但是我找不到第一张照片之后拍摄的其他照片。

但是我可以在终端中使用adb查找所有照片。
有人有建议吗?

我像这样拍照:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mCameraFilePath = MediaDataManager.getInstance().getFilePath(MediaDataManager.IMAGE, fileName);
// this function returns the img file path, like /storage/emulated/0/Android/data/<package-name>/files/Pictures/1556592144304.png
mCameraPicUri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".fileprovider", new File(mCameraFilePath));
cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraPicUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);

并保存照片:
try {
    File file = new File(=getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName + ".png");
    if (file.exists()) {
         file.delete();
         file.createNewFile();
    }
     InputStream inputStream = =getContentResolver().openInputStream(mCameraPicUri);
     FileOutputStream out = null;
     try {
         out = new FileOutputStream(file);
         if (inputStream != null) {
               copy(inputStream, out);
               inputStream.close();
         }

         if (out != null) {
             out.close();
         }
     } catch (FileNotFoundException e) {
         e.printStackTrace();
     } catch (IOException e) {
          e.printStackTrace();
     }
}

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCameraPicUri);
sendBroadcast(mediaScanIntent);

这是copy()方法:
private static final int EOF = -1;
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
private static long copy(InputStream input, OutputStream output) throws IOException {
     long count = 0;
     int n;
     byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
     while (EOF != (n = input.read(buffer))) {
         output.write(buffer, 0, n);
         count += n;
     }
     return count;
}

解决方案:
我应该这样创建uri:
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
mCameraPicUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

最佳答案

解决方案:我应该这样创建uri:

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
mCameraPicUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

关于android - 如何拍照并保存在Android Q中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55896479/

10-13 05:14