I'M Getting a Nullpointerexception When I Use Action_Image_Capture to Take a Picture

I'm getting a NullPointerException when I use ACTION_IMAGE_CAPTURE to take a picture

Turns out the stock camera application doesn't send EXTRA_OUTPUT, which is why it's null. However, some camera apps (like the hero) do. Awesome.
So the answer is to specify EXTRA_OUTPUT. The nexus one camera app will save the image to that location. Then in onActivityResult() check if the intent is null. If it isn't, use data.getData(), and if it is then use the location specific in EXTRA_OUTPUT via a constant and insert it into the Mediastore. Urgh.

Null pointer after capturing image using android camera

try below code,

        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

File file=getOutputMediaFile(1);
picUri = Uri.fromFile(file); // create
i.putExtra(MediaStore.EXTRA_OUTPUT,picUri); // set the image file

startActivityForResult(i, CAPTURE_IMAGE);

where getOutputMediaFile(int) will be,

/** Create a File for saving an image */
private File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyApplication");

/**Create the storage directory if it does not exist*/
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}

/**Create a media file name*/
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == 1){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".png");
} else {
return null;
}

return mediaFile;
}

and finally,

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Intent i;
switch (requestCode) {
case CAPTURE_IMAGE:
//THIS IS YOUR Uri
Uri uri=picUri;
break;
}
}
}

Cheers....:)

NullPointerException on intent when accessing Camera Android

Found an answer here:https://stackoverflow.com/a/37628687/10941659. It is not made clear on the android developer training site, but when you want to get a full size image you don't use the intent that you pass to onActivityResult. You need to use the path that you generate for your image, for example:

private void takePictureAndUpload() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
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
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"com.example.android.provider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}

}

protected void onActivityResult(int requestCode, int resultCode, final Intent data){
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == REQUEST_IMAGE_CAPTURE) && (resultCode == Activity.RESULT_OK)){
count++;
Bitmap imageBitmap = null;
try {
imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),photoURI);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
final byte[] imageData = stream.toByteArray();

setuId(user);
final String path = "posts/" + UUID.randomUUID() + ".jpg";

FirebaseStorage storage = FirebaseStorage.getInstance();
final StorageReference storageRef = storage.getReference();
final StorageReference imageRef = storageRef.child(path);

UploadTask uploadTask = imageRef.putBytes(imageData);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String ex = e.getLocalizedMessage();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
imageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
{
@Override
public void onSuccess(Uri downloadUrl)
{
final String url = downloadUrl.toString();//do something with downloadurl
addPhotoUrlToDatabase(post.getImageUrl_1(), post.getImageUrl_2(), path);
}
});
}
});
}
}

NullPointerException when I try to capture an image (built-in camera) and save it to a file

You are passing MediaStore.EXTRA_OUTPUT, in which case, the intent field in onActivityResult can be null.

The solution is to just use the photo file you created, and passed as uri in the putExtra

So instead of trying to get the photo file location from the intent, just use Uri.fromFile(photoFile)

replace

    extras = data.getExtras();
...

with

   `Uri.fromFile(photoFile)`

or just use photoFile if that is what you want...just dont rely on intent data

Android camera capture activity returns null Uri

You have to tell the camera, where to save the picture and remeber the uri yourself:

private Uri mMakePhotoUri;

private File createImageFile() {
// return a File object for your image.
}

private void makePhoto() {
try {
File f = createImageFile();
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMakePhotoUri = Uri.fromFile(f);
i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
startActivityForResult(i, REQUEST_MAKE_PHOTO);
} catch (IOException e) {
Log.e(TAG, "IO error", e);
Toast.makeText(getActivity(), R.string.error_writing_image, Toast.LENGTH_LONG).show();
}
}

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_MAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
// do something with mMakePhotoUri
}
return;
default: // do nothing
super.onActivityResult(requestCode, resultCode, data);
}
}

You should save the value of mMakePhotoUri over instance states withing onCreate() and onSaveInstanceState().

null pointer exception - image capture

dispatchTakePictureIntent(ACTION_TAKE_PHOTO_B);
startActivity(intent);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
mAlbumStorageDirFactory = new FroyoAlbumDirFactory();
} else {
mAlbumStorageDirFactory = new BaseAlbumDirFactory();
}

Here dispatchTakePictureIntent() indirectly calls a function getAlbumDir()that uses mAlbumStorageDirFactory which is not initialized yet, as observed in the logcat:

08-26 01:45:55.845: E/AndroidRuntime(3471): Caused by: java.lang.NullPointerException
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.getAlbumDir(ResultActivity.java:47)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.createImageFile(ResultActivity.java:69)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.setUpPhotoFile(ResultActivity.java:76)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.dispatchTakePictureIntent(ResultActivity.java:135)
08-26 02:33:57.415: E/AndroidRuntime(3730): at com.example.camoid.ResultActivity.onCreate(ResultActivity.java:171)

Set up mAlbumStorageDirFactory before calling dispatchTakePictureIntnet().

NullPointerException on taking a picture and save as URI

Are you sure your package name is com.example.cameraapplicationfiles?

I think it should be Android/data/com.example.cameraapplication/files/Pictures

if not try something like this.

for the authorities use full string name
android:authorities="com.myCameraDemo.app".

ans for the file_paths.xml <external-path name="site_images" path="Android/data/com.myCameraDemo.app/files/Pictures" />

if it works then you know where the problem is.

Unable to save a photo captured with the native camera app

This answers a very similar question:
I'm getting a NullPointerException when I use ACTION_IMAGE_CAPTURE to take a picture

Simplest solution is to keep an instance variable with the file name at the time when you start the activity, and load the file name from that instance variable during onActivityResult.

Exception delivering result on ACTION_IMAGE CAPTURE (taking photo from third party app)

Assuming you initialised mImg somewhere else (since you try to call setText on it), try checking if selectedImage is null. The fact that getData() would return null was a known bug on some devices, and I'm not sure it's been fixed.



Related Topics



Leave a reply



Submit