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);
Related
I'm developing an Android App that uses an Intent to open the in-built camera, and take a photo that then should be saved to storage and be sent to a different intent that crops it.
I've run the app a few times but can't see any of the photos being saved anywhere in storage. I started debugging the code and found that it was skipping out on the commands in the OnActivityResult method because the resultCode parameter was always going back as 0.
Out of curiosity I manually changed this to the required value of -1, and the code then skipped out on the next if statement because data.getExtras() (data being the Intent parameter) was returning null, even though I had been adding an extra in the code that triggered the Camera Activity.
I can't quite figure out what's happening here. I'd assume the image is saving somehow because no exception is being thrown, but don't see what might be causing the empty values of resultCode and data.
My code is shown below. If anyone can give me a point in the right direction, that would be a massive help!
Thanks,
Mark
public void onClick(DialogInterface dialogInterface, int i) {
if (optionItems[i].equals(OPTION_CAMERA)) {
//intent to open device camera
Intent cameraIntent =
new Intent(
MediaStore.ACTION_IMAGE_CAPTURE
);
//create 'Spond' folder inside photo directory
//target folder for image to be saved
File imagesFolder =
new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES),
getString(R.string.app_name)
);
//create directory if it doesn't already exist
imagesFolder.mkdirs(); //bool return value not needed
//create file with unique id
File image = createFileAtUniquePath(imagesFolder);
//use authority for AndroidManifest to create permission
//to get uri from temporary file in app
String fileProviderAuthority = getApplicationContext().getPackageName() +
getString(R.string.authorities_fileprovider);
Uri uriSavedImage = FileProvider.getUriForFile(
getApplicationContext(),
fileProviderAuthority,
image
);
//pass URI to intent so it will be available in activity result
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
//grant read/write permissions with file
cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cameraIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(cameraIntent, REQUEST_CAMERA);
The OnActivityResult code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
if (data.getExtras() != null) {
newAccountPhotoUri = (Uri) data.getExtras().get(MediaStore.EXTRA_OUTPUT);
newAccountPhotoFileName = getFileNameFromURI(newAccountPhotoUri);
if (!newAccountPhotoFileName.equals("")) {
appAccountPhotoChanged = true;
//send image to be cropped
cropImage(newAccountPhotoUri);
}
}
}
//...
}
I am using code from http://developer.android.com/training/camera/photobasics.html
Code:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = FileUtilities.createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(getActivity(),"Error!",Toast.LENGTH_SHORT).show();
}
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras(); //error
//code after this doesn't get executed
}
}
I'm trying to get access to the thumbnail and the picture being stored on the device. But for some reason when I try the code, I get a null pointer exception on the data.getExtras(); part.
What am I missing here?
That's one of notorious Android development experience.
Android Intent does not guarantee to give captured image in data.getExtras(), especially user utilize 3rd party camera/imaging app. You can find many trials and suggestions in here and anywhere googled with "android camera intent null".
Some common of them are as below.
data.getExtras().get("data");
data.getExtras() with different key (i.e "photo")
data.getData()
Uri.fromFile(f) for EXTRA_OUTPUT predefined path.
Uri.fromFile(f) with some random filename (datetime format or IMG-xxx) without maintaining EXTRA_OUTPUT definition.
I recommend you to find it using breakpoint which route of the variable that the intent given. It would be good to check all of them in if-else if-else approach.
In addition, check out crash report carefully after releasing the app. You may get the error out of the above trials.
To get the ThumbNail, you don't need to create a file etc. please try this code below.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
and to get the results.
#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");
imgView.setImageBitmap(imageBitmap);
}
}
imgView is the ImageView you want to set the ThumbNail to.
In case if you want to create a file and then try this, [ which is not needed for a Thumbnail], you may want to try adding the following permission to manifest as you are trying to read and write to storage.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
All the best.
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.
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..
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.