Paths Issue while retrieving a photo - android

i have a problem with paths while taking a photo and setting it to an ImageView.
when i use this method
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
the path used is
02-01 12:07:24.068 31618-31618/disco.unimib.it.polapp D/ciao: /storage/emulated/0/Android/data/disco.unimib.it.polapp/files/Pictures
but when i assign the uri like this
photoUri = FileProvider.getUriForFile(NotifyActivity.this, "com.example.android.fileprovider", photoFile);
uri shows
02-01 12:07:24.071 31618-31618/disco.unimib.it.polapp D/ciao: content://com.example.android.fileprovider/images/Android/data/disco.unimib.it.polapp/files/Pictures/JPEG_20180201_120724_1953425230.jpg
and when i try to save photo to a variable, the error is
02-01 12:07:29.975 31618-31618/disco.unimib.it.polapp E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /images/Android/data/disco.unimib.it.polapp/files/Pictures/JPEG_20180201_120724_1953425230.jpg (No such file or directory)
here's the full code
NotifyActivity.java:
public class NotifyActivity extends AppCompatActivity {
Bitmap image;
String mCurrentPhotoPath;
File photoFile=null;
Uri photoUri;
Intent TakePictureIntent;
ImageView photoSaved;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notify);
final Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar4);
setSupportActionBar(toolbar);
setTitle(R.string.problem);
final Button button3=(Button) findViewById(R.id.button3);
final Button button4=(Button) findViewById(R.id.button4);
photoSaved=(ImageView) findViewById(R.id.photoSaved);
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TakePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (TakePictureIntent.resolveActivity(getPackageManager()) != null) {
if (ContextCompat.checkSelfPermission(NotifyActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(NotifyActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
} else {
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
photoUri = FileProvider.getUriForFile(NotifyActivity.this, "com.example.android.fileprovider", photoFile);
Log.d("ciao", String.valueOf(photoUri));
TakePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(TakePictureIntent, 50);
}
}
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Snackbar.make(findViewById(R.id.notify),R.string.notificationsent,Snackbar.LENGTH_LONG);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
image= BitmapFactory.decodeFile(photoUri.getPath());
if(image!=null){
Snackbar.make(findViewById(R.id.notify),R.string.photosaved,Snackbar.LENGTH_LONG).show();
photoSaved.setImageBitmap(image);
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Log.d("ciao", String.valueOf(getExternalFilesDir(Environment.DIRECTORY_PICTURES)));
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
#Override
public void onRequestPermissionsResult(int requestCode, String [] permissions,int [] grantResults){
if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
try{
photoFile = createImageFile();
} catch(IOException ex){
ex.printStackTrace();
}
if (photoFile != null) {
photoUri = FileProvider.getUriForFile(NotifyActivity.this, "com.example.android.fileprovider", photoFile);
TakePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(TakePictureIntent, 50);
}
}else{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
file_paths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="images" path="." />
</paths>
i have alredy put the provider in my androidmanifest.xml

java.io.FileNotFoundException is thrown because you are using FileProvider's Uri, which creates
content:// Uri for a file.
While BitmapFactory.decodeFile(String path) takes
complete path name for the file to be decoded.
So, you need to use:
BitmapFactory.decodeFile(photoFile.getAbsolutePath());

<external-path. Change to <external-files-path.
image= BitmapFactory.decodeFile(photoUri.getPath());
Replace by
InputStream is = getContentResolver().openInputStream(photoUri);
image= BitmapFactory.decodeStream(is);

Related

Can not save picture from intent

I have a project witch have permission to camera and storage.
When the user click in some buttons the camera will be open by intent and take picture then saved it in the device to add it to firebase storage.
This operation was successful on most devices, but in Redmi A8(Android 10 , MIUI 12.0.1) it
was failed , the camera open then the application stop , maybe it failed to save the picture or failed to get it in ActicityResult.
Who I can solve it ?
Note: This is a part of methods in my code.
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(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getActivity(),
"com.apps.android.fileprovider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}
}
}
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,
".jpg",
storageDir
);
currentPhotoPath = image.getAbsolutePath();
return image;
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
File f = new File(currentPhotoPath);
//showImage.setImageURI(Uri.fromFile(f));
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
getActivity().sendBroadcast(mediaScanIntent);
uploadImageToFirebase(f.getName(), contentUri);
}
}
}
private void uploadImageToFirebase(String name, Uri contentUri) {
final StorageReference image = storageReference.child("pictures/" + name);
image.putFile(contentUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
image.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference().child("Shifts").child(empId+"").
child(shiftKey).child(shiftImageType).setValue(uri+"");
alertDialog.dismiss();
}
});
startActivity(new Intent(getActivity() , MainActivity.class));
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getActivity(), e.getMessage()+"", Toast.LENGTH_SHORT).show();
}
});
}

Android: How to store image from camera and show the in gallery?

I'm new in Android and I would create app that uses camera, store image taken from camera in device and show it in gallery? Anyone have any advice on how to do it? For now I have created the activity that allows you to take pictures but I don't know how to proceed to save and show the photos taken from the camera in the gallery. Please help me, I'm very desperate. Thanks in advance to everyone.
This is my code from android documentation:
public class CamActivity extends AppCompatActivity {
private ImageView imageView;
private Button photoButton;
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);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(checkPermissions()) {
dispatchTakePictureIntent();
galleryAddPic();
}
}
});
}
#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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File storageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), "Camera");
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;
}
}
}
allow camera and storage permission
public class CamActivity extends AppCompatActivity {
private ImageView imageView;
private Button photoButton;
private String currentPhotoPath;
private File photoFile = null;
private static final String TAG = "CamActivity";
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.taken_photo);
photoButton = findViewById(R.id.btnCaptureImage);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
captureImage();
}
});
}
#SuppressLint("QueryPermissionsNeeded")
private void captureImage() {
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (pictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(pictureIntent, 100);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK) {
if (data != null && data.getExtras() != null) {
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
saveImage(imageBitmap);
imageView.setImageBitmap(imageBitmap);
}
}
}
private void saveImage(Bitmap bitmap) {
String filename;
Date date = new Date(0);
#SuppressLint("SimpleDateFormat")
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
filename = sdf.format(date);
try {
String path = Environment.getExternalStorageDirectory().toString();
OutputStream outputStream = null;
File file = new File(path, "/MyImages/"+filename + ".jpg");
File root = new File(Objects.requireNonNull(file.getParent()));
if (file.getParent() != null && !root.isDirectory()) {
root.mkdirs();
}
outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outputStream);
outputStream.flush();
outputStream.close();
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (Exception e) {
Log.e(TAG, "saveImage: " + e);
e.printStackTrace();
}
}
}

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;
}
}
}

Using camera to take photo and save to gallery

I have went through several documentation and stacks, however I'm not quite sure how do I implement this...
And help or sample codes would really help me understand more.
Here are the sets of codes that runs the camera and it's working perfectly fine, my next question is, how do I let it automatically saved into phone gallery?
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tvCameraTiles = (TextView) findViewById(R.id.tvCameraTiles);
tvCameraTiles.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
dispatchTakePictureIntent();
}
});
}
private void dispatchTakePictureIntent()
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
//Get Camera Image File Data
// Bundle extras = data.getExtras();
// Bitmap imageBitmap = (Bitmap) extras.get("data");
// mImageView.setImageBitmap(imageBitmap);
}
}
have you read this https://developer.android.com/training/camera/photobasics.html ?
Especially the part:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
You don't seem to save the photo in the external storage, so it should work.
EDIT: I tried to make a really basic application following the documentation with the method galleryAddPic.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button photoButton = (Button) findViewById(R.id.photobutton);
photoButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
dispatchTakePictureIntent();
}
});
}
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
//Bundle extras = data.getExtras();
//Bitmap imageBitmap = (Bitmap) extras.get("data");
//mImageView.setImageBitmap(imageBitmap);
galleryAddPic();
}
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
File storageDir = Environment.getExternalStorageDirectory();
File image = File.createTempFile(
"example", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
This is working for me! I take a picture using the camera app, and then it's showed in the gallery app.
I modified the code regarding the FileProvider part, now it passes the Uri with the help of the method Uri.fromFile(photoFile). I save the photo in a different position too, check the code in the method createImageFile for this one.
In the Android Studio emulator is working fine, let me know about you.
I tried out Rex B code snippet, but ended up getting a error on Android Studio.
android.os.FileUriExposedException: file:///storage/emulated/0/example6941495009290613124.jpg exposed beyond app through ClipData.Item.getUri()
If anyone got this error, I found a simple fix
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
I added these two lines to the onClick method
Button photoButton = (Button) findViewById(R.id.photobutton);
photoButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
dispatchTakePictureIntent();
}
});
}
I know this is old post, but I'm sure someone will come by this one day.
First the file will be created and the ByteStream will be written on the file and saved. you don't do anything for this file to be present in the gallery. This will automatically refreshed by gallery.
I have experimented for past 1 week with most of the stackoverflow threads but couldn't find a comprehensive solution, but all the threads helped me. Here is the working code which I have tested in Android 11,12 and 13(Google Pixex 6a)
AndroidManifest.xml will have the following
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
This what I have mentioned in provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
call the below method on onclick event
dispatchTakePictureIntent();
In onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case STApplication.SHOW_TAKEPICTURE:
//Uri selectedImage=null;
if (resultCode == Activity.RESULT_OK) {
try{
//Bundle extras = data.getExtras();
Bitmap imageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), OldURI);
//galleryAddPic();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try {
saveImageInAndroidApi29AndAbove(imageBitmap);
} catch (Exception e) {
//show error to user that operatoin failed
}
} else {
saveImageInAndroidApi28AndBelow(imageBitmap);
}
btnPicture.setText(getString(R.string.picture_taken));
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
Then finally include all the below methods and variables inside your activity class
String currentPhotoPath;
Uri ImageURI;
public static final int SHOW_TAKEPICTURE = 99;
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,
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
ImageURI=photoURI;
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, SHOW_TAKEPICTURE);
}
}
}
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
currentPhotoPath = image.getAbsolutePath();
return image;
}
private boolean saveImageInAndroidApi28AndBelow(Bitmap bitmap) {
OutputStream fos;
String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();
File image = new File(imagesDir, "IMG_" + System.currentTimeMillis() + ".png");
try {
fos = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 95, fos);
Objects.requireNonNull(fos).close();
} catch (IOException e) {
e.printStackTrace();
//isSuccess = false;
return false;
}
//isSuccess = true;
return true;
}
#NonNull
public Uri saveImageInAndroidApi29AndAbove(#NonNull final Bitmap bitmap) throws IOException {
final ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME, "IMG_" + System.currentTimeMillis());
values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
}
final ContentResolver resolver = getApplicationContext().getContentResolver();
Uri uri = null;
try {
final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
uri = resolver.insert(contentUri, values);
if (uri == null) {
//isSuccess = false;
throw new IOException("Failed to create new MediaStore record.");
}
try (final OutputStream stream = resolver.openOutputStream(uri)) {
if (stream == null) {
//isSuccess = false;
throw new IOException("Failed to open output stream.");
}
if (!bitmap.compress(Bitmap.CompressFormat.PNG, 95, stream)) {
//isSuccess = false;
throw new IOException("Failed to save bitmap.");
}
}
//isSuccess = true;
return uri;
} catch (IOException e) {
if (uri != null) {
resolver.delete(uri, null, null);
}
throw e;
}
}

Upload image error: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference

I have a problem with "capture image with camera", and store it into Firebase. I think that the code is right, because it worked with "select image from gallery". The app stoped after capturing the image, and it didn't store it into the database. I think that it is a problem for android M and N (Android 6 and 7). There is also the error in logcat.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){
mPregresDialog.setMessage("Uploading...");
mPregresDialog.show();
Uri uri = data.getData();
StorageReference filepath = mStorage.child("Photo").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
mPregresDialog.dismiss();
Toast.makeText(MainActivity.this, "Upload Done", Toast.LENGTH_LONG).show();
}
});
}}
logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference
The problem is that your Uri does not get the image as it should so you need to create your own Uri for your image you capture.
You need to go to android documentation: https://developer.android.com/training/camera/photobasics.html#TaskPhotoView
For your app, you can also just copy and paste this code in your main activity:
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, CAMERA_REQUEST_CODE);
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
storage = FirebaseStorage.getInstance().getReference();
b_gallery = (Button) findViewById(R.id.b_gallery);
b_capture = (Button) findViewById(R.id.b_capture);
iv_image = (ImageView) findViewById(R.id.iv_image);
progressDialog = new ProgressDialog(this);
b_capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
dispatchTakePictureIntent();
}
});
}
#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(photoURI).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();
}
});
}
}
}
And make sure you follow the documentation and have added the provider in your AndroidManifest.xml:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths"></meta-data>
</provider>
And the res/xml/file_paths.xml (you just make a directory under "res" folder and name it xml, then you create a resource file and name it file_paths.xml); then delete all the code inside it (it'll have some lines) and paste the following:
<?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.serjardovic.firebasesandbox/files/Pictures" />
</paths>
Make sure you CHANGE the com.serjardovic.firebasesandbox to your own package name!

Categories

Resources