Android camera intent file into imageview NPE (SGS3) - android

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

Related

Android data.getData() returns null from CameraActivity for some phones

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

Intent extras are null when picking an image from gallery

I have been researching on this but I am not able to find an answer for this.
I am picking an image from the gallery using media store intent and I am able to get the image file path in onActivityResult method. (I know how to get the URI in the intent and filepath).
I am passing in some intent extras on starting the activity (startActivityForResult) but all the intent extras are null.
Code snippets (in case):
This is my onActivityResult code which is working and i get the image path
/* On activity result from image button */
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("Result Code" + resultCode);
if(requestCode == FavoriteListAdapter.IMAGE_PICK_CODE && data != null && data.getData() != null && resultCode == FragmentActivity.RESULT_OK) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
//Link to the image
String imageFilePath = cursor.getString(0);
System.out.println("imagefilepath" + imageFilePath);
System.out.println(data.getStringExtra("exp"));
cursor.close();
}
}
I am starting my activity with startActivityForResult
Intent imageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
imageIntent.setType("image/*");
imageIntent.putExtra("exp", "testing");
((FragmentActivity)view.getContext()).startActivityForResult(imageIntent, IMAGE_PICK_CODE);
I should be able to get the string "testing" in onActivityForResult but all I get is null.
Any ideas and suggestions will be appreciated. THnkas a lot.
Actually I figured it out .. When you are sending an intent to a system activity like MediaStore or the camera etc... the onActivityResult will not have the intent extras you sent while calling the activity.
This is probably by design and will only contain the extras given by the system activity. For instance after picking an image from the gallery, the returning intent from the gallery will only the URI containing the image path.
Same goes to camera or any system activites.

ImageView setImage in Android 4.0

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.

take picture from android camera and send it to web server

im build some apps that call camera activity..
im just to take picture from my apps and send it to web server..
but i can't get my image path..
im always getting NullException Error when try to get image path..
here's my code when calling camera activity :
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
this.startActivityForResult(camera, PICTURE_RESULT);
and this is code for activity result :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICTURE_RESULT){
if (resultCode == Activity.RESULT_OK) {
takePicture(data);
} else if (resultCode == Activity.RESULT_CANCELED) {
}
}
}
protected void takePicture(Intent data) {
Bundle b = data.getExtras();
pic = (Bitmap) b.get("data");
if (pic != null) {
imagePicture.setImageBitmap(pic);
}
}
is there something wrong with my code?
Thanks
Ok, I see your problem. You're not setting the path to begin with. Please look at this doc.
http://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE
So you see, when you call ACTION_IMAGE_CAPTURE you are not passing it the extra EXTRA_OUTPUT that tells the application where the picture is going to be stored. This EXTRA_OUTPUT is the path to the file.
So right under where you make the intent do this:
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
URI pictureUri = Uri.fromFile(new File(<path to your file>));
camera.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

how to upload the image taken by calling camera activity from android phone?

hi i have an image which is taken from andorid by calling image _capture
how do i upload it to a windows server?
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Toast toast = Toast.makeText(this,"camera cancelled", 10000);
toast.show();
return;
}
// lets check if we are really dealing with a picture
if (requestCode == 0 && resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap b = (Bitmap) extras.get("data");
//setContentView(R.layout.main);
ImageView mImg;
mImg = (ImageView) findViewById(R.id.head);
mImg.setImageBitmap(b);
// save image to gallery
String timestamp = Long.toString(System.currentTimeMillis());
MediaStore.Images.Media.insertImage(getContentResolver(), b, timestamp, timestamp);
}
Here's exactly what you need to do How to send HTTP POST request and receive response?
You may take a look at this article also http://www.theserverside.com/news/1365153/HttpClient-and-FileUpload. POST upload example should help.
I'd suggest you to let the image capture activity be called from the gallery activity. The reason is you will have full sized image that'll be stored on default location, so When you finish gallery activity, you will have path to that full sized image. Intent is not designed to pass the huge file to another activity. Also I've seen that image taken by the camera (by android.media.action.IMAGE_CAPTURE) are of small sized. So refer to my blog that helps you to complete image capture and uploading tasks.

Categories

Resources