-
Notifications
You must be signed in to change notification settings - Fork 52
Open
Description
If I take a pic from camera, and after this, I try to take a pic from gallery, the app crashes, and send me this error in log:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
My code, that's being used is this below:
public class MainActivity extends AppCompatActivity {
@BindView(R.id.imageView1)
ImageView ivPhoto;
private static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap imgBmpFromCamera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.imageView1)
void takeImage() {
alert(this, "Choose an option", "Open with camera or take from gallery",
"Camera", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CAMERA)) {
} else {
// LIBERA A PERMISSÃO SEM PEDIR NOVAMENTE
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_IMAGE_CAPTURE);
}
} else {
openCamera();
}
}
},
"Gallery", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
ImagePicker.pickImage(MainActivity.this, "Select your image:");
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Problem to open the gallery.\nUse the camera or try later.", Toast.LENGTH_SHORT).show();
Log.e("LOG", "Erro: " + e);
}
}
});
}
private void openCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
// =================================================================================
public static void alert(Context c, String t, String m, String msgBtnPositive, DialogInterface.OnClickListener cliqueOk, String msgBtnNegative, DialogInterface.OnClickListener cliqueNegative) {
AlertDialog.Builder a = new AlertDialog.Builder(c);
a.setTitle(t);
a.setMessage(m);
a.setPositiveButton(msgBtnPositive, cliqueOk);
a.setNegativeButton(msgBtnNegative, cliqueNegative);
a.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// camera
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
if (extras != null) {
try {
ivPhoto.setImageBitmap((Bitmap) extras.get("data"));
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
// gallery
try {
ivPhoto.setImageBitmap(ImagePicker.getImageFromResult(this, requestCode, resultCode, data));
} catch (Exception e) {
Log.i("LOG", "onActivityResult: " + e);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_IMAGE_CAPTURE:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
}
break;
default:
break;
}
}
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels