In my app I am trying to set an ImageView's image to an image that was just taken with the camera. My problem is that it works on my old Droid (Android 2.2), but not on my Droid Razr (Android 4.0). I was wondering if anyone could help me figure out why.
Here is the camera Intent when the Take Photo button is clicked:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String imageFileName = System.currentTimeMillis() + ".jpg";
File photo = new File(Environment.getExternalStorageDirectory(),
imageFileName);
imageUri = Uri.fromFile(photo);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, ACTIVITY_CAMERA);
Here is the Activity's result:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ACTIVITY_CAMERA) {
if(resultCode == Activity.RESULT_OK){
imageView.setImageUri(imageUri);
}
}
}
The ImageView remains blank on the Razr.
Weird how looking at the logcat is helpful.
Anyway, I was getting "bitmap is too large" when trying to set the ImageView's image. Even before this issue I tried Google's solution here. However, the bitmap is always null and I haven't looked into it enough to figure out why.
EDIT: I got Google's solution to work. For the decodeFile() method, I was passing in my Uri as a string: imageUri.toString(). I changed this to imageUri.getPath() and it is now working.
Related
Here is my intent code for select picture and cropping from gallery.
int PICK_IMAGE_REQUEST = 100;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 150);
intent.putExtra("aspectY", 150);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
getActivity().startActivityForResult(Intent.createChooser(intent,
"Complete using with."), PICK_IMAGE_REQUEST);
Here is my onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
int PICK_IMAGE_REQUEST = 100;
Bundle extras = null;
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
extras = data.getExtras();
}
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ImageView profilePhoto = (ImageView) findViewById(R.id.profileImageView);
profilePhoto.setImageBitmap(photo);
}
These codes above crop&set image successfully. However, sometimes it doesn't work properly. I mean when I'm using 3rd party gallery app instead of using device's default gallery app. It doesn't set the image. This may be not getting file path correctly when using another gallery app. So , how can I implement select&crop and set image into imageview ? I researched on to internet but nothing solved this problem so far.
Here is my intent code for select picture and cropping from gallery.
No, that is code for selecting a picture. The various extras that you have on there are not part of the ACTION_PICK documentation, or any other official documentation, for that matter.
These codes above crop&set image successfully
Not generally.
However, sometimes it doesn't work properly. I mean when I'm using 3rd party gallery app instead of using device's default gallery app.
There are thousands of Android device models. There is no single "default gallery app" for all of them; there will be dozens, if not hundreds, of "default gallery app" implementations. None have to support the random extras that you are trying. Also, none have to return something in a data extra, as ACTION_PICK returns a Uri in the result Intent, as is covered in the documentation for ACTION_PICK.
So , how can I implement select&crop and set image into imageview ?
Get rid of the extras. Get rid of the extras.getParcelable("data") bit. Get the Uri of the picked image (data.getData()). Use that in conjunction with one of various image cropping libraries available for Android.
I have a fatal error occurring in my onActivityResult coming back from a camera activity. What has me scratching my head is that the error is only happening on a handful of phones (based on the number of affected users) while there seems to be nothing wrong for the majority. I can duplicate the error on my Nexus 6 (running Lollipop 5.1.1) while my Note 5 (also 5.1.1) has no problems at all.
The problem is when I am trying to assign the imageUri from data.getData(). Debugging on the Note 5, data.mData equals "content://media/external/images/media/2215" while on the Nexus 6, data.mData is null.
I know this is a common question asked on SO but I haven't found anything that has helped me so far. Can anyone point me to the solution for this and provide an answer?
Method Starting Camera Activity for Result
#OnClick(R.id.change_image_camera) public void takePicture(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);}
onActvityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri imageUri = data.getData(); //The trouble is here
String realPath = Image.getPath(this, imageUri); //getPath crashes because imageUri is null
Image.compressImage(realPath);
File file = new File(realPath);
Bundle extra = new Bundle();
extra.putString("URL", realPath);
returnIntent.putExtras(extra);
setResult(RESULT_OK, returnIntent);
finish();
}
}
I greatly appreciate any help on this one!
Uri imageUri = data.getData(); //The trouble is here
There is no requirement for a camera app to return a Uri pointing to the photo, as that is not part of the ACTION_IMAGE_CAPTURE Intent protocol. Either:
Supply EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, in which case you know where the image should be stored, and do not need to rely upon getData(), or
Use the data extra in the response Intent, which will be a Bitmap of a thumbnail of the image
Your next bug is here:
String realPath = Image.getPath(this, imageUri);
There is no requirement that the image be stored as a file that you can access, unless you provide a path to that location via EXTRA_OUTPUT.
if(resultCode == RESULT_OK){
Bitmap bitmap = (Bitmap) imageReturnedIntent.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
I am trying to include a camera in my app that saves the files locally on the SD card. The camera application starts, but the resultCode is always 0. I have added the following permissions to my Manifest:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Here is the code for my the camera:
#SuppressLint("SimpleDateFormat")
private void takePicture(){
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/resources/resources/WI1");
SimpleDateFormat timeStampFormat = new SimpleDateFormat("MM/dd/yyyy");
String image_name =username +"-"+ timeStampFormat.format(new Date())+".png";
File image = new File(imagesFolder, image_name);
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
int request_code = 100;
startActivityForResult(imageIntent, request_code);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(this,"Error Saving Image, please throw device at wall", Toast.LENGTH_SHORT).show();
} // end on activity result
What's causing the bug?
Thanks!
EDIT: I removed the previously posted logcat information, as it was not relevant to this issue.
EDIT2:
I half solved the issue, if I use this code the camera works just fine. Could someone tell me what would cause that?
private void takePicture(){
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/resources/resources/WI1");
String image_name = "matt"+image_count+".png";
image_count+=1; // this is at the moment useless.
File image = new File(imagesFolder, image_name);
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
int request_code = 100;
startActivityForResult(imageIntent, request_code);
}
EDIT 3:
The issue is with the timeStampFormat, if I exclude it the camera works just fine. Could someone explain why? If I'm not mistaken, it's because the date format I chose has forward slashes in it.
I was having this same error - resultCode was always 0. Turns out that after I took the picture in the camera app, I was clicking the X on the bottom right instead of the checkmark in the bottom center.
it is coming 0 because you have not set result code in the activity , suppose if i call activity b from a .. and on activity b i set setReuslt(reuslt_ok) , then only onactivity result will get the result code as result_ok.. by default the result code is 0
since you are opening the internal camera activity of android , so you are not setting your result code there, so when camera activity finishes it returns the default code back to you
There's lots of questions on the same topic, however none of them provided me an answer.
I've tried a lot of solutions, yet none of them is working for me so far.
I'm invoking the camera intent in a fragment and inserting uri to the newly created file where I want the picture to be stored and then on activityresult I'm passing the uri to a new activity where I want to show it in an imageview and then proceed to upload it.
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File mImageFile = new File(Environment.getExternalStorageDirectory().getPath()+"/DCIM/" + "Camera/" + File.separator+System.currentTimeMillis()+".jpg");
imageUri = Uri.fromFile(mImageFile);
Log.d("camera", "imageUri: " + imageUri.toString());
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent, CAMERACODE);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case CAMERACODE:{
if (resultCode == Activity.RESULT_OK) {
Intent intent = new Intent(getActivity(), SubmitActivity.class);
intent.putExtra("imageuri", imageUri.toString());
getActivity().startActivity(intent);
and in SubmitActivity:
Bundle extras = getIntent().getExtras();
imageUri = extras.getString("imageuri"); <-- imageUri has the correct path, and the actual file is created and the photo is there. The photo is not showing up on gallery for some reason though, only when browsing the folders. What is the reason for this?
iv = (ImageView) findViewById(R.id.selectedpicture);
iv.setImageURI(Uri.parse(imageUri)); <-- this line causes a NullPointerException.
Can anyone explain the reason for why the setImageURI fails? Any alternative way of doing this? I'm using Samsung Galaxy S3
I believe iv is null and there is no problem with imageuri.
Check whether you have done setContentView and check if the layout has selectedpicture imageView.
Edit:
The photo is not showing up on gallery for some reason though
Gallery uses MediaStore to get all the photos on the sdcard. But when you take a picture MediaStore db would not have been updated. For the pic to show up in gallery MediaScanning should be run. Check this post
I use following code take a Picture from camera and to obtain picture's path.
...
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_IMAGE_CAPTURE); // image capture
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult:" + resultCode + " request:" + requestCode);
switch (requestCode) {
case CAMERA_IMAGE_CAPTURE:
Uri selectedImageUri = data.getData();
userImagePath = getPath(selectedImageUri);
break;
}
}
It works good on emulator and on different devices. But on Samsung Galaxy Nexus(4.0.2) it does not launches Camera app. But it returns RESULT_OK to onActivityResult and I see no exceptions in LogCat.
Please give me and advice how to solve this issue.
Thanks in advance!
You are missing EXTRA_OUTPUT, which may impact matters. My Galaxy Nexus can run this sample project successfully, which uses the following code to request the picture:
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output = new File(dir, "CameraContentDemo.jpeg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);