This question already has an answer here:
I wonder how to avoid the app from crashing if no image is inserted into SQLite ?
In Activity B, there has one save button, one open camera button and imageView.
save button used to return the image to A, open camera button used for user to select picture and imageView placed the selected image.
In Activity A , it has a submit button, used to save the image which was returned from B to SQLite.
Activity B
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent returnIntent = new Intent();
returnIntent.putExtra("photo", photo);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(selectedImage, filePathColumn, null, null,
null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap a = (BitmapFactory.decodeFile(picturePath));
photo = scaleBitmap(a, 200, 200);
imageView.setImageBitmap(photo);
}
break;
case REQUEST_IMAGE_CAPTURE:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
String fileName = "tempimg.jpg";
try {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Activity A
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { // button is clicked
SB.insertStaffBenefit(images, a); // SB is the object of StaffAPI ...this line has error
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
}
LogCat Error
java.lang.NullPointerException
at com.example.project.myapplication.API.StaffAPI.getBitmapAsByteArray(StaffAPI.java:75)
at com.example.project.myapplication.API.StaffAPI.insertStaffBenefit(StaffAPI.java:60)
at com.example.project.myapplication.GUI.AddClaims$4.onClick(AddClaims.java:167)
at android.view.View.performClick(View.java:4230)
The logCat shows that I didn't insert any image to SQLite. How can I write an if-else condition so that the app will not crashed when no image is inserted ?
StaffAPI
public long insertStaffBenefit(ArrayList<ImageAndText> imageText,long id)
{
Bitmap image=i.getImage();
byte[]data=getBitmapAsByteArray(image); // error
values.put(MyDatabaseHelper.Image, data);
database.insert(MyDatabaseHelper.TABLE_STAFF_BENEFIT, null, values);
}
database.close();
return 0 ;
}
public static byte[] getBitmapAsByteArray(Bitmap bitmap)
{ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream); // error
return outputStream.toByteArray();
}
Thanks
Aucun commentaire:
Enregistrer un commentaire