如何在没有插入图像时避免应用程序崩溃? [英] How to avoid app from crashed when no image is inserted?

查看:63
本文介绍了如何在没有插入图像时避免应用程序崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果没有图像插入到SQLite中,如何避免应用程序崩溃?



在活动B中,有一个保存按钮,一个打开的摄像头按钮和imageView。



保存按钮用于将图像返回到A,打开相机按钮用于用户选择图片和图像放置所选图像。



在活动A中,它有一个提交按钮,用于保存从B返回给SQLite的图像。



活动B



 save.setOnClickListener( new  View.OnClickListener(){
@ Override
public void onClick(查看v){

Intent returnIntent = new Intent();
returnIntent.putExtra( photo,照片);
setResult(Activity.RESULT_OK,returnIntent);
完成();
}
});

@ Override
protected void onActivityResult( int requestCode, int resultCode,
意图数据){
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();
位图a =(BitmapFactory.decodeFile(picturePath));
photo = scaleBitmap(a, 200 200 );
imageView.setImageBitmap(photo);
}
break ;

case REQUEST_IMAGE_CAPTURE:
位图缩略图=(位图)data.getExtras()。get( data);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90 ,bytes);
字符串 fileName = tempimg.jpg ;

尝试 {
photo =(Bitmap)data.getExtras()。get( data);
imageView.setImageBitmap(photo);
} catch (例外e){
e.printStackTrace();
}
}
}





活动A



 btnSubmit.setOnClickListener( new  View.OnClickListener(){ 
@覆盖
public void onClick(查看v){ // 按钮点击
SB.insertStaffBenefit (图像,a); // SB是StaffAPI的对象...此行有错误
Intent intent = new Intent(getApplicationContext(),MainActivity。 class );
startActivity(intent);

}
});

}





LogCat错误



 java.lang.NullPointerException 
at com.example.project.myapplication.API.StaffAPI.getBitmapAsByteArray(StaffAPI.java:< span class =code-digit> 75 )
at com.example.project.myapplication.API.StaffAPI.insertStaffBenefit(StaffAPI.java:< span class =code-digit> 60 )
在com.example.project.myapplication.GUI.AddClaims $ 4.onClick(AddClaims.java: 167
在android.view .View.performClick(View.java: 4230





logCat显示我没有向SQLite插入任何图像。如何编写if-else条件,以便在没有插入图像时应用程序不会崩溃?



StaffAPI



  public   long  insertStaffBenefit(ArrayList< ImageAndText> imageText, long  id)
{

Bitmap image = i.getImage();
byte [] data = getBitmapAsByteArray(image); // 错误
values.put(MyDatabaseHelper.Image,data);
database.insert(MyDatabaseHelper.TABLE_STAFF_BENEFIT,null,values);
}
database.close();
return 0 ;

}

public static byte [] getBitmapAsByteArray(位图位图)
{ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0 ,outputStream); // 错误
返回 outputStream.toByteArray();
}







谢谢

解决方案

4.onClick(AddClaims.java: 167
在android.view.View.performClick(View.java:< span class =code-digit> 4230 )





logCat显示我没有插入任何图像到SQLite。如何编写if-else条件,以便在没有插入图像时应用程序不会崩溃?



StaffAPI



  public   long  insertStaffBenefit(ArrayList< ImageAndText> imageText, long  id)
{

Bitmap image = i.getImage();
byte [] data = getBitmapAsByteArray(image); // 错误
values.put(MyDatabaseHelper.Image,data);
database.insert(MyDatabaseHelper.TABLE_STAFF_BENEFIT,null,values);
}
database.close();
return 0 ;

}

public static byte [] getBitmapAsByteArray(位图位图)
{ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0 ,outputStream); // 错误
返回 outputStream.toByteArray();
}







谢谢


< blockquote>只需检查 i.getImage(); 是否返回null。


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

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

解决方案

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

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


Just check to see if i.getImage(); returns null.


这篇关于如何在没有插入图像时避免应用程序崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆