Error during taking picture - android

I'm writing an application which have to take a picture and send it to a web service.
There is the code I use to :
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
outputFileUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/appicture.jpg"));
Log.i("URI", outputFileUri.toString());
i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i, cameraData);
The URI looks like :
file:///sdcard/appicture.jpg
But if I put "outputFileUri" var in the i.putExtra, my app just quit.
If not, I can take the picture but I can't get her URI then unable to send it to my webservice.
EDIT 1 :
Error log ( on the activity resylt)
06-26 09:17:46.108: I/Cam error(699): java.lang.NullPointerException
EDIT 2 :
If I remove the "outputFileUri", I correctly got the Image. But, then I'm unable to convert the Bitmap into File to be able to send it.
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
_bmp = (Bitmap) extras.get("data");
}
EDIT 3 :
The problem was from
_bmp = (Bitmap) extras.get("data");
And the picture is correctly save in the sd card.

Add WRITE_EXTERNAL_STORAGE permission in Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
EDIT:
if you are passing Image Uri in Intent for Capturing image then get image as in onActiityResult as:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == cameraData) {
File picture = new File(Environment.getExternalStorageDirectory() + "/appicture.jpg");
Uri imguri=Uri.fromFile(picture);
}
super.onActivityResult(requestCode, resultCode, data);
}
and if you want to get image as data in onActiityResult then Launch Camra as:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, cameraData);
in onActivityResult:
onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == cameraData) {
Bundle extras = data.getExtras();
_bmp = (Bitmap) extras.get("data"); //Get data here
}
}
}

If you already added the permissions and the path is correct. Could you please try removing ".jpg" at the end of your path

Please add this in Manifest File
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Happy Coding..

Related

(android) taking picture without saving

I used the code below to call camera in my android app.
With the code, I could take a picture and get the absolute path of the picture.
But the problem is that the absolute path always pointed the internal storage.
That is, the phone automatically saved the picture I took and I had to erase them one by one.
I just want the absolute path so that I can turn it into a File object without saving in my storage or SD card.
Maybe, I can temporarily save it in my allocated memory.
Is there any way to do this?
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PICK_FROM_CAMERA);
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != RESULT_OK)
return;
if(requestCode == PICK_FROM_CAMERA){
imageUri = data.getData();
Log.d("메시지", "uri = "+imageUri);
Cursor c = this.getContentResolver().query(imageUri, null, null, null, null);
c.moveToNext();
absolutePath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
Glide.with(this).load(imageUri).into(image);
You can take a thumbnail of image without store into internal memory or SD card:
#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);
}
}
For full sized image you need to define shared file (for example: cache file at SD card) and put it as MediaStore.EXTRA_OUTPUT:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// camera app will save photo into sharedFileURI path
intent.putExtra(MediaStore.EXTRA_OUTPUT, sharedFileURI);
startActivityForResult(intent, PICK_FROM_CAMERA);
See Save the Full-size Photo article of official documentation for more info

How to get name of captured image in android onActivityResult

how to get image name from captured image/intent data. any one suggest me. i need through intent data only
Here is my code:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, PIC_FROM_CAMERA);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && data != null){
Bitmap photo = (Bitmap) data.getExtras().get("data");
}
}
AS from the Android documentation here you could not get any name for your image since android wont name for your file but you could get the time when the image was taken and the dimension of the image from This answer You could name the image by yourself and send that bitmap.

How to capture image thumbnail AND save file in a custom folder in Android

I am trying to capture an image from an existing camera application, save the image in a customized folder, and display the thumbnail in and imageView. The camera supplies the thumbnail as long as I haven't specified where to save the file:
I can get the thumbnail from the returned intent:
...
Intent i = = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i)
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
Bitmap mImageBitmap = (Bitmap) extras.get("data");
}
Or I can save the file in a specified folder (which works fine)
...
Intent i = = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra((MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i)
}
but the thumbnail is no longer stored in the intent extra "data", and when I try to retrieve the thumbnail, I get an error (this is from my LogCat)
10-04 06:30:14.463: E/AndroidRuntime(1967): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity: java.lang.NullPointerException
As you can see, the field returned is null instead of the bitmap thumbnail. I have tried decoding the bitmap afterwards to generate a thumbnail from the file directly, but it takes too long (even when downsampled I get out of memory error) , and it seems counterintuitive to do the job twice. Any suggestions?
Okay. If you are passing an outputURI to the intent then you will not be able to receive the data back from the intent in onActivityResult().
I think only option is to use the same outputURI to display the thumbnail..
Try this.
void captureImage(){
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/MyFolder", "myImage"+ ".jpg");
mCapturedImagePath = file.getAbsolutePath();
Uri outputFileUri = Uri.fromFile(file);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i, CAMERA_REQUEST);
}
onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
File file = new File(mCapturedImagePath);
imageView.setImageURI(Uri.fromFile(file));
}
}
}
Your Bitmap mImageBitmap is a local variable, make that global if you want to use it outside the onActivityResultFunction otherwise set the image there as
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
try this
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

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 get the images from device in android java application

In my application I want to upload the image.
For that I have to get images from gallery in android device.
How do I write code that accomplishes this?
Raise an Intent with Action as ACTION_GET_CONTENT and set the type to "image/*". This will start the photo picker Activity. When the user selects an image, you can use the onActivityResult callback to get the results.
Something like:
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri chosenImageUri = data.getData();
Bitmap mBitmap = null;
mBitmap = Media.getBitmap(this.getContentResolver(), chosenImageUri);
}
}

Categories

Resources