I use intent with action android.provider.MediaStore.ACTION_IMAGE_CAPTURE
to capture image and get a path of the the image. After got the path, I decode it to a bitmap.
Bitmap photoBitmap = BitmapFactory.decodeFile(currentPhotoPath);
All work well on Device1, the log for image path look like this :
/storage/emulated/0/Pictures/QPOS/IMG_20160622_110127_1473532607.jpg
Then I run the app to a new device call Device2. The image path I got after capture image:
/storage/emulated/0/Pictures/QPOS/IMG_20160622_105436_2068195440.jpg
It look normal. But the decode method return a null bitmap at this line
Bitmap photoBitmap = BitmapFactory.decodeFile(currentPhotoPath);
I do not know why have different between two device, I build on the same source code. Can you explain why I got null bitmap for method decode file ? And how to fix this issue ? Thanks.
Update: I add request permission for API 23 by this method:
private void requestPermissions() {
Log.d("binh", "request Permision");
if (Build.VERSION.SDK_INT >= 23) {
List<String> permissionRequestList = new ArrayList<>();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
permissionRequestList.add(android.Manifest.permission.CAMERA);
}
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
permissionRequestList.add(android.Manifest.permission.ACCESS_FINE_LOCATION);
}
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
permissionRequestList.add(android.Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
permissionRequestList.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
permissionRequestList.add(android.Manifest.permission.READ_PHONE_STATE);
}
if (permissionRequestList.size() > 0) {
ActivityCompat.requestPermissions(this, permissionRequestList.toArray(new String[permissionRequestList.size()]), Constants.REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
}
This is a code to create path for the image:
private File setUpPhotoFile() throws IOException {
File f = createImageFile();
currentPhotoPath = f.getAbsolutePath();
return f;
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
File albumF = getAlbumDir();
File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, albumF);
return imageF;
}
private File getAlbumDir() {
File storageDir = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
//storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), getString(R.string.app_name));
storageDir = new File(
getExternalFilesDir(Environment.DIRECTORY_PICTURES), getString(R.string.app_name));
if (storageDir != null) {
if (!storageDir.mkdirs()) {
if (!storageDir.exists()) {
Log.d("CameraSample", "failed to create directory");
return null;
}
}
}
} else {
Log.d(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");
}
return storageDir;
}
After build and granted permission from dialog, I got the same issue.