android File.createTempFile() fails with Environment.getExternalStorageDirector() - android

I'm trying to write an app which takes a photo and save it into the gallery.
I've followed the Android Developers' guide, but it only shows how to same images into a private directory.
Here is the code I use to send the intent to the camera
public void dispatchTakePictureIntent(View view) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
2121);
return;
}
//File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = null;
try {
image= File.createTempFile("IMG", ".jpg", storageDir);
//tempImage=image;
} catch (IOException e) {
errorMessage.setVisibility(View.VISIBLE);
Log.e("error", Log.getStackTraceString(e));
return;
}
//photoURI = FileProvider.getUriForFile(this, "prova.fileprovider", image);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null ) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
When I run it on the Android Studio emulator (Pixel_API_27) I get the error when calling the File.createTempFile
Thank you in advance for any help.

For Android 6+ you have to add code to ask the user to confirm the read write permissions you requested in manifest.
Google for runtime permissions.
You are #1246 this year who is posting this problem.

I find a solution by working around the problem.
I make the camera intent to save the picture to the private app folder,
then I move the file to the public one. Here is the code
public void dispatchTakePictureIntent(View view) {
//ask write permission
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
2121);
return;
}
//get path to private folder
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
//create temp-file where the image will be saved
File image = null;
try {
image= File.createTempFile("IMG", ".jpg", storageDir);
} catch (IOException e) {
errorMessage.setVisibility(View.VISIBLE);
Log.e("error", Log.getStackTraceString(e));
return;
}
Image = image;
//get uri of the image
Uri photoURI = FileProvider.getUriForFile(this, "prova.fileprovider", image);
//send the intent to the camera
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null ) {
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
Then when the picture is take I do
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { //AFTER TAKING PICTURE
File publicImage = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + Image.getName());
Image.renameTo(publicImage); //here we move the file to the public directory
Image = publicImage;
//optionally one can update the gallery, but I don't need it
}

Related

Android 11 scooped storage manage_external_storage permission is not accepted by google play console

my app is working fine below the 10 version but android 11 and higher versions do not support external storage. my picker is not picking any document file. but after giving manage_external_storage permission in manifest google play store did not approve my app. please help if you know any alternate solution for all file access permission.
Try like below. Create global variable as needed or refactor as per your need. This is tested upto Android 11.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
private void OpenCamera() {
try {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = createFile();
} catch (Exception ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getActivity().getApplicationContext(), getActivity().getApplicationContext().getPackageName() + ".fileprovider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, 3);
}
// }
} catch (Exception e) {
}
}
private File createFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, /* prefix */".jpg", /* suffix */storageDir /* directory */);
// Save a file: path for use with ACTION_VIEW intents
ImageLoc = image.getAbsolutePath();
return image;
}
// Open Gallery
private void OpenGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), 1);
}
// onActivityResult handles gallery pics, camera pics and pdf.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
if (data.getData() != null) {
try {
Uri imageUri = data.getData();
File file = new File(Path_from_Uri.getPath(getActivity(), imageUri));
} catch (Exception e) {
Toast.makeText(getActivity(), "Please Select Image from Gallery", Toast.LENGTH_LONG).show();
}
}
}
} else if (requestCode == 3) {
try {
File file = new File(ImageLoc);
} catch (Exception e) {
Log.d("TAG", e.toString());
}
}
}

Android: How to save image from camera to a specific folder and view it in my device's gallery?

as described in the title I would like to save the image from camera in a specific folder and then view it in the gallery of my device. So far I can access the camera, take the photo and show it in an ImageView in the same layout. How could I do? I read on the net to use the provider but using it, the images taken with the camera are not displayed in the gallery of my device. Thanks in advance to those who will answer.
This is my code:
public class CamActivity extends AppCompatActivity {
private ImageView imageView;
private Button photoButton;
private Button saveToGallery;
private String currentPhotoPath;
private File photoFile = null;
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_camera);
imageView = findViewById(R.id.taken_photo);
photoButton = findViewById(R.id.btnCaptureImage);
saveToGallery = findViewById(R.id.gallery_save);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(checkPermissions()) {
dispatchTakePictureIntent();
galleryAddPic();
}
}
});
saveToGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bitmap myBitmap = BitmapFactory.decodeFile(photoFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
} else {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(CamActivity.this, "error" + ex.getMessage(), Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.myapp.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
private boolean checkPermissions() {
//Check permission
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
//Permission Granted
return true;
} else {
//Permission not granted, ask for permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_IMAGE_CAPTURE);
return false;
}
}
}

Getting a picture from an external file directory

I followed the Take photo guide of the Android developer documentation to take a picture. I want to use this picture for the Firebase MLKit, but I just need it temporarily, so I saved it to my private storage and delete if after I used it (I am extracting it's text).
So, I am using an Intent to let the capture be taken for me and save it to a specific file. If the Intent is done, how do I extract that picture so I can pass the bitmap to the Firebase API?
In the earlier stated documentation, it says
Note: If you saved your photo to the directory provided by getExternalFilesDir(), the media scanner cannot access the files because they are private to your app.
But it does not say how to access that file. That's my question: How do I access it?
For all interested in my code so far:
public class MainActivity extends AppCompatActivity {
static final int REQUEST_TAKE_PHOTO = 1;
private Uri photoURI;
//ToDo: delete picture at the end of app usage [finally->delete]
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
//ToDo:Logging/Toast msg
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"mypackagename",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
} else {
//ToDo: logging/Toast msg
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == REQUEST_TAKE_PHOTO){
//This is where I need to access the picture I saved.
}else{
//ToDo: logging/Toast msg
}
}
public void scanText(View view) {
//Firebase stuff, not relevant
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
#SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat
("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
}
Add this line to get the image bitmap
if(resultCode == Activity.RESULT_OK && requestCode == REQUEST_TAKE_PHOTO)
{
//This is where I need to access the picture I saved.
// get the image bitmap from this code
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
}
else
{
//ToDo: logging/Toast msg
}

How get Image bitmap in onActivityResult and Delete image after image set in imageview

I am trying to open camera in Marshmallow and nougat using FileProvider but not getting the bitmap to show captured image in imageview in onActivityResult method.Even i tried to get bitmap from image path. Here is my code
image_click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
}
static final int REQUEST_TAKE_PHOTO = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"com.xyz.dummyapp.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
try{
Bitmap bitmap = BitmapFactory.decodeFile(photoURI.getPath());
mImageView.setImageURI(photoURI);
File file=new File(mCurrentPhotoPath);
if (file.exists()){
if (file.delete()){
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),"Deleted",Toast.LENGTH_SHORT).show();
}
});
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.xyz.dummyapp/files/Pictures" />
</paths>
I don't know if your code doesn't contain everything. But here are some points to note:
You should save photos in the device public directory
Generally, any photos that the user captures with the device camera should be saved on the device in the public external storage so they are accessible by all apps.
(Taken from the docs)
If you do so - you need to declare the WRITE_EXTERNAL_STORAGE in your AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
With Android Marshmallow you have then request the permission for that. Here is a simplified version of that:
.
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 99);
...
....
#Override
public void onRequestPermissionsResult(final int requestCode, #NonNull final String[] permissions, #NonNull final int[] grantResults) {
// Execute your code to create the file and start the camera intent here
}
(Read more here)
You have also to use the getExternalStoragePublicDirectory from Environment as storageDir
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Your file_path.xml should now point to Pictures/:
<external-path name="my_pictures" path="Pictures/"/>
In your onActivityResult you can simply use the photoUri field which you received from FileProvider#getUriForFile to set to the image:
imageView.setImageURI(photoUri);
For me the changes helps to make a working example:
I think you have to use this library for camera and gallery : https://github.com/jrvansuita/PickImage you don't needs to convert image in to bitmap.and this library is working fine in Marshmallow and nougat.

Uploading image (ACTION_IMAGE_CAPTURE) to Firebase storage [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
EDIT: The discussion on this issue has been carried to another question: Image capture with camera & upload to Firebase (Uri in onActivityResult() is null) PLEASE check it out! I did some manipulations (following the android documentation) but still getting same problem.
So here I'm trying to do a simple task of capturing an image with camera and uploading it to my storage database on Firebase. I have the following button to do the job:
b_capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
and the following code for the upload:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){
progressDialog.setMessage("Uploading...");
progressDialog.show();
Uri uri = data.getData();
StorageReference filepath = storage.child("Photos").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(MainActivity.this, "Upload Successful!", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(MainActivity.this, "Upload Failed!", Toast.LENGTH_SHORT).show();
}
});
}
}
in the app I'm able to take picture, but after I confirm the picture taken the APP CRASHES and I get the following error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference
So, I understand that the "Uri" does not have anything in it, but I'm confused about what can be done. Need help.
And yes, I've checked all permissions, Firebase connections, Gradle sync etc.
You need to re-read the documentation and follow the example to create a file and store its Uri in the image capture intent. This is the code from the documentation you need to add:
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
...
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
b_capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dispatchTakePictureIntent()
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
Question solved in the following discussion: Image capture with camera & upload to Firebase (Uri in onActivityResult() is null) Thanks to everyone who participated!

Categories

Resources