如何拍照并保存在Android Q中?

问题描述 投票:1回答:1

新编辑:

我正在开发一个Android应用程序,并希望支持android Q.当我的应用程序在目标API(<= 28)中运行时,一切正常。但在android Q中,当我拍照并尝试保存时,会发生一些奇怪的事情。

当我拍摄第一张照片时,我可以在自定义照片选取器中找到它。但我找不到第一张照片后拍摄的其他照片。

但我可以在终端使用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);
android photo androidq
1个回答
0
投票

解决方案:我应该像这样创建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);
© www.soinside.com 2019 - 2024. All rights reserved.