I am following the steps from the official documentation: https://developer.android.com/training/camera/photobasics#TaskPhotoView
I have done everything the same but, I receive the error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { }} to activity {com.rrgt19.partyfinder/com.rrgt19.partyfinder.activity.AddActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java: 4324) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367) at android.app.ActivityThread.-wrap19(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164)
Permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
FileProvider:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
file_paths:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="my_images"
path="Android/data/com.rrgt19.partyfinder/files/Pictures" />
</paths>
Code:
public class AddActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView mPhotoImageView;
public static final int REQUEST_CODE_TAKE_PHOTO = 1;
private String mCurrentPhotoPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// Views
mPhotoImageView = findViewById(R.id.photoImageView);
// Listeners
mPhotoImageView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == mPhotoImageView) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
225);
}
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
226);
}
} else {
dispatchTakePictureIntent();
}
}
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
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) {
Uri photoURI = FileProvider.getUriForFile(AddActivity.this, "com.example.android.fileprovider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_TAKE_PHOTO && resultCode == RESULT_OK) {
Bundle extras = data.getExtras(); // Aquí es null
Bitmap imageBitmap = (Bitmap) extras.get("data");
mPhotoImageView.setImageBitmap(imageBitmap);
}
}
}
Currently, I manage to pull the photo correctly but, when I click OK, to go to my Activity, the App closes. I am using Android Nougat 7.0 on the emulator and on my personal phone.
Any ideas?
First of all, it is important to comment that your application must use the following permissions:
It is important to require the permissions manually if the device is running OS 6.0 or later, call this method before using the camera:
Regarding the error you mention in your question:
it originates here:
I recommend you do this method to get the Uri of the file:
and when taking the photo make this change to
onActivityResult()
, where you will receive the photo via getContentResolver() :In this way the camera will open without problem:
and you will be able to add the image taken by the camera without problem inside your
ImageView
:This would be the code of your complete class:
In fact, I had the same problem that you mention, I recommend you check this application:
https://github.com/Jorgesys/TakePicture
You have to save the file name when you save the image.
To take a photograph in an ImageView of the APP with low quality, it has worked for me with the following code.
The solution is to put this like this
convert to String the
photoURI
in theputextra
, so the apk does not close when taking the photo.