Taking the full-size image from a camera app - java

My App has an option to take photos from the camera. I'm using phone's default camera.
The problem is when I take the picture, in my onActivityResult, I get null from intent(data). so it shows the message Picture wasn't taken!".
Code to call Intent:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Create a File reference to access to future access
photoFile = getPhotoFileUri(photoFileName);
// wrap File object into a content provider
// required for API >= 24
// See https://guides.codepath.com/android/Sharing-Content-with-Intents#sharing-files-with-api-24-or-higher
Uri fileProvider = FileProvider.getUriForFile(MainActivity.this, "com.example.android.fileprovider", photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileProvider);
// If you call startActivityForResult() using an intent that no app can handle, your app will crash.
// So as long as the result is not null, it's safe to use the intent.
if (intent.resolveActivity(getPackageManager()) != null) {
// Start the image capture intent to take photo
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
});
}
onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null) {
// by this point we have the camera photo on disk
Bitmap takenImage = BitmapFactory.decodeFile(String.valueOf(photoFile));
// RESIZE BITMAP, see section below
// Load the taken image into a preview
img.setImageBitmap(takenImage);
} else { // Result was a failure
Toast.makeText(this, "Picture wasn't taken!", Toast.LENGTH_SHORT).show();
}
}
Make a file for the photo:
public File getPhotoFileUri(String fileName) {
// Only continue if the SD Card is mounted
if (isExternalStorageAvailable()) {
// Get safe storage directory for photos
// Use `getExternalFilesDir` on Context to access package-specific directories.
// This way, we don't need to request external read/write runtime permissions.
File mediaStorageDir = new File(
getExternalFilesDir(Environment.DIRECTORY_PICTURES), APP_TAG);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){
Log.d(APP_TAG, "failed to create directory");
}
// Return the file target for the photo based on filename
File file = new File(mediaStorageDir.getPath() + File.separator + fileName);
return file;
}
return null;
}
provider tag in 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"/>
</provider>
file_paths.xml
<?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.example.houman.myapplication/files/Pictures" />
</paths>
After "Taking Photos Simply | Android Developers"
didn't help me so much, I tried Accessing the Camera and Stored Media | CodePath
but now I have this problem.

I get null from intent(data)
You are not using data, and in the EXTRA_OUTPUT scenario there is no need for the camera app to supply an Intent in the result. So, get rid of that check, by replacing:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null) {
with:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

Related

Android Image FIle variable is becoming null after image capture

The app I am currently developing has the feature of being able to capture an image, record a video, browse and upload a media file from gallery. Problem is, the app works well enough in emulator. Without any errors (checked with android 9+ emulators). But the app behaves unexpectedly while testing on physical device (Android 10). Unexpected, because, video recording and playing works fine, as well as browsing and uploading media from gallery. What fails is image capture. The thing is the image file is actually getting created, But I am getting the image file variable photoFile value as null in onActivityResult() method.
Here is the implementation:
manifest.xml
<provider android:name=".utils.MyFileProvider"
android:authorities="com.app.mtccrm.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<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>
method to create image_file
public static File createImageFile(Context mContext, String name) throws IOException {
String imageFileName = name.replaceAll(" ", "_");
File storageDir = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = new File(storageDir + File.separator + imageFileName + ".png");
return image;
}
method responsible for opening the camera
private void openCamera() {
if (ActivityCompat.checkSelfPermission(AddNewFileActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION_CODE_IMAGE);
} else {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
photoFile = CommonMethod.createImageFile(this, fileName);
} catch (IOException ioe) {
ioe.printStackTrace();
}
//Toast.makeText(this, ""+photoFile.getAbsolutePath(), Toast.LENGTH_SHORT).show();
if(photoFile != null ) {
Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", photoFile);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(captureIntent, TAKE_PICTURE_CODE);
}else {
Toast.makeText(this, "Something went wrong while trying to create a file. Please try again", Toast.LENGTH_SHORT).show();
}
}
}
onActivityResult()
Toast.makeText(this, ""+photoFile, Toast.LENGTH_SHORT).show();
if (requestCode == TAKE_PICTURE_CODE && resultCode == RESULT_OK) {
if(photoFile == null) {
if(data != null) {
parseGalleryFile(data);
}
}else {
setImage(Uri.parse(photoFile.getAbsolutePath()));
}
}
The first Toast in onActivityResult() gives me null value. So somehow the photoFile variable is becoming null, however I am at a loss about the cause. Since, from the same activity using the same methods, recording of videos and browsing gallery works just fine. I thought it might have something to do with the activity getting paused when the camera is called. So, I added this
#Override
public void onSaveInstanceState(#NonNull Bundle outState, #NonNull PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
Toast.makeText(this, ""+photoFile, Toast.LENGTH_SHORT).show();
if(photoFile != null) {
outState.putString("FileId", photoFile.getAbsolutePath());
}
}
#Override
protected void onRestoreInstanceState(#NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Toast.makeText(this, ""+savedInstanceState.getString("FileId"), Toast.LENGTH_SHORT).show();
if(savedInstanceState.getString("FileId") != null) {
if (photoFile == null) {
photoFile = new File(savedInstanceState.getString("FileId"));
}
}
}
Unfortunately, save and restore state methods are not getting called, so that means the activity is not being paused. So what could be the cause for photofile becoming null? what exactly am I doing wrong?
Thanks for the help.

Android Application Crashes while Capturing Full Size Picture

I am developing an Android Application that takes a picture and saves a full-size picture onto the device. I have mostly taken help from developers android site. When I run my code and try to take the picture the application crashes instantly. The code I am using is as follows:
static final int REQUEST_IMAGE_CAPTURE = 1;
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
System.out.println("Error in Dispatch Take Picture Intent");
}
// 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);
}
}
}
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");
//imageView.setImageBitmap(imageBitmap);
Toast.makeText(getApplicationContext(),"Working",Toast.LENGTH_LONG).show();
}
}
String currentPhotoPath;
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 View.OnClickListener onCamClick() {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Starting Camera Activity");
//dispatchTakePictureIntent();
//Toast.makeText(getApplicationContext(),"Working",Toast.LENGTH_LONG).show();
dispatchTakePictureIntent();
}
};
}
When I run my code I get the following error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.zaeem.tia, PID: 15468
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.zaeem.tia/files/Pictures/JPEG_20200301_003227_1043176843378455577.jpg
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
at com.zaeem.tia.app.activities.MainActivity.dispatchTakePictureIntent(MainActivity.java:121)
at com.zaeem.tia.app.activities.MainActivity.access$000(MainActivity.java:40)
at com.zaeem.tia.app.activities.MainActivity$1.onClick(MainActivity.java:164)
at android.view.View.performClick(View.java:7339)
at android.widget.TextView.performClick(TextView.java:14221)
at android.view.View.performClickInternal(View.java:7305)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27787)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7076)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
filepath.xml file:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Android/data/com.zaeem.tia.app.activities/files/Pictures" />
</paths>
Please advise regarding this matter.
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
Here, you are saying that the authority string for your FileProvider is com.example.android.fileprovider. Based on the error, that is not what you have on the <provider> element in the manifest.
add this in your manifest.xml (after ):
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="android.getqardio.com.gmslocationtest"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
Create provider_paths on Resources/xml, and add this content:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
name="share"
path="external_files"/>
</paths>
Activity:
File imagePath = new File(getFilesDir(), "external_files");
imagePath.mkdir();
File imageFile = new File(imagePath.getPath(), "test.jpg");
// Write data in your file
Uri uri = FileProvider.getUriForFile(this, getPackageName(), imageFile);
Intent intent = ShareCompat.IntentBuilder.from(this)
.setStream(uri) // uri from FileProvider
.setType("text/html")
.getIntent()
.setAction(Intent.ACTION_VIEW) //Change if needed
.setDataAndType(uri, "image/*")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);

How to pick an image from gallery and save within the app after cropping? [duplicate]

This question already has answers here:
android:select image from gallery then crop that and show in an imageview
(3 answers)
android pick images from gallery
(19 answers)
Closed 4 years ago.
I am new to Android development and I tried my best to pick an image from gallery and saving within the app after cropping but I failed. Please help me to solve this issue. I tried to mix up different codes but nothing is working for me.
if (resultCode == RESULT_OK) {
//Uri photoUri = data.getData();
//if (photoUri != null) {
// photoPickerIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
CropImage.activity(android.net.Uri.parse(data.getDataString()))
.setAspectRatio(1,1)
.setFixAspectRatio(true)
.start(activity);
CropImage.ActivityResult result1 = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
iv.setImageURI(result1.getUri());
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result1.getError();
Log.d(TAG, "onActivityResult: " + error.getMessage());
}
//currentImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
//selectedImage.setImageBitmap(currentImage);
// }
First of All Add a dependency inside your project Gradle.built(app:odle) file
Like
dependencies {
implementaion 'com.theartofdev.edmodo:android-image-cropper:2.8.+'
}
After that add following two permission inside manifiest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Create a Constant inside your class
private static final int REQUEST_FOR_GALLARY = 1;
you will need this .
After that Put the following Code inised your button on which u click to open gallery or whatever your are using .
Intent gallaryIntent = new Intent();
gallaryIntent.setAction(Intent.ACTION_GET_CONTENT);
gallaryIntent.setType("image/*");
startActivityForResult(gallaryIntent, REQUEST_FOR_GALLARY);
After that Override the medthod onActivityResult of your Activity class
Like
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_FOR_GALLARY && resultCode == RESULT_OK && data !=
null) {
Uri imageUri = data.getData();
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
//Save image wherever you want to save it
}
}
}
Now Change your Code According to your Recuirement and Save image whereever you want to store .

File path is empty for google drive files

I am developing a file uploading android application. My objective is to upload the user selected file from file manager to a remote server. But when a google drive file is selected , file uploading fails because of empty path . Can somebody help me ?
My code is :
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_FILE_REQUEST) {
if (data == null) {
//no data present
return;
}
Uri selectedFileUri = data.getData();
selectedFilePath = FilePath.getPath(mActivity, selectedFileUri);
if (selectedFilePath != null && !selectedFilePath.equals("")) {
callUploadDocumentAPI();
} else {
Toast.makeText(mActivity, StringConstants.CANT_UPLOAD_TO_SERVER, Toast.LENGTH_SHORT).show();
}
}
}
But when a google drive file is selected , file uploading fails because of empty path .
FilePath.getPath(mActivity, selectedFileUri) cannot work. A Uri is not a file.
Use a ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. Either use that InputStream directly, or use it to make your own copy of the content in some file that you control, then use that copy.

Take photo, display photo thumbnail in new activity upon success

Right now I've got an app that takes a photo via an intent. After taking the photo, I want the user to be able to confirm it and push the photo to another activity where they can add details. I'm struggling to find the correct way to pass the photo from activity to activity.
Right now, confirming the photo brings the user back to the main activity.
Here's my relevant code:
Main Activity
public void takePhoto(View view) {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
Log.d("MySecondApp",fileUri.toString());// create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
if (data!=null) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
}
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MySecondApp");
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MySecondApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
I would try something like
if(resultCode == Activity.RESULT_OK)
handleCameraPhoto(data);
handle method:
private void handleCameraPhoto(Intent intent) {
Bundle extras = intent.getExtras();
Bitmap mImageBitmap = (Bitmap) extras.get("data");
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", mImageBitmap);
startActivity(intent);
}
and then, retrieve it in your other activity
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
and assign it to your ImageView
ImageView iv = (ImageView)findViewById(R.id.myImageView);
iv.setImageBitmap(bitmap);
Make a constructor in your new class that takes an intent or even the image:
class newClass {
public newClass(Intent intent){
...
}
...
}
And then just make the intent the size of a thumbnail in this new class where you can do the confirmation in a popup or something similar and then have the information to be filled on the page for this class. This will also be better because if they wanted to retake the picture you could have this class which they are taken to after the picture is taken if not accepted go to take another picture.
Take a look at this blog post I wrote on how to take a full size image and a thumbnail version as well:
Use Camera Activity for Thumbnail and Full Size Image
after you did that you can extract the file path of the saved file from the file instance using getAbsolutePath() method and pass it to the following Activity.

Categories