NEW EDIT:
I'm developing an android application and want to support android Q. When my app runs in target API(<= 28), everything works normally. But in android Q, when i take a photo and try to save it, something strange happens.
When I take the first photo, I can find it in my custom photo picker. But I can't find the other photos taken after the first one.
But I can find all the photos by using adb in terminal. Anyone has some suggestions?
I take photo like this:
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);
and save photo:
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);
Here is the copy() method:
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;
}
SOLUTION: I should create the uri like this:
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);
SOLUTION: I should create the uri like this:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With