Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Java
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

Java
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
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 didnt 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

Java
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
Posted

1 solution

Just check to see if i.getImage(); returns null.
 
Share this answer
 
Comments
wseng 22-Dec-15 5:18am    
If I have select an image, it will not crashed. If not, it will crashed. So I have to make
an if-else statement?
Richard MacCutchan 22-Dec-15 5:36am    
OK, so make one, how difficult is it to test an object reference for null?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900