我无法在sqlite android中插入图像的代码有什么问题?

时间:2019-06-18 14:42:43

标签: android database sqlite

当我单击屏幕上的btnD时,出现“ NOT SUCC”。当我删除用于插入图像的代码时,则数据成功添加。来自textView的数据出现在数据库中。我的代码有什么问题?

这是我的活动,其中有Buttons和textViews。

public class AddInData extends AppCompatActivity {

   EditText edtName, edtCity, edtLocation, edtCategory;
    Button btnImg, btnD, btnLists;
    ImageView imageView;

    final int REQUEST_CODE_GALLERY = 100;

    DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_in_data);
        databaseHelper = new DatabaseHelper(this);

        edtName = findViewById(R.id.edtIme);
        edtCity = findViewById(R.id.edtGrad);
        edtLocation = findViewById(R.id.edtLokacija);
        edtCategory = findViewById(R.id.edtKategorija);
        btnImg = findViewById(R.id.btnSlika);
        btnD = findViewById(R.id.btnDodadi);
        btnLists= findViewById(R.id.btnLista);
        imageView = findViewById(R.id.slikaView);


        btnImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityCompat.requestPermissions(
                        AddInData.this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        REQUEST_CODE_GALLERY
                );
            }
        });


        AddData();
    }

    public void AddData(){

        btnD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean isInserted = databaseHelper.insertData(
                        edtName.getText().toString(),
                        edtCity.getText().toString(),
                        edtLocation.getText().toString(),
                        edtCategory.getText().toString(),
                        imageViewToByte(imageView));
                if(isInserted){
                    Toast.makeText(getApplicationContext(),"Added succ ", Toast.LENGTH_SHORT).show();
                   // edtName.setText("");
                   // edtCity.setText("");
                   // edtLocation.setText("");
                   // edtCategory.setText("");
                   // imageView.setImageResource(R.mipmap.ic_launcher);
                } else{
                    Toast.makeText(getApplicationContext(),"NOT succ ", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
    public static byte[] imageViewToByte(ImageView image) {
        Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        return byteArray;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(requestCode == REQUEST_CODE_GALLERY){
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, REQUEST_CODE_GALLERY);
            } else {
                Toast.makeText(getApplicationContext(), "You don't have permission to access file location!", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode == REQUEST_CODE_GALLERY && resultCode == RESULT_OK && data != null){
            Uri uri = data.getData();
           try{
                InputStream inputStream = getContentResolver().openInputStream(uri);

                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                imageView.setImageBitmap(bitmap);
            }catch (FileNotFoundException e){
                e.printStackTrace();

            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}

SQLiteOpenHelper:

    public static final String DATABASE_NAME = "RestaurantDB.db";
    public static final String TABLE_NAME = "restaurants_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "CITY";
    public static final String COL_4 = "LOCATION";
    public static final String COL_5 = "CATEGORY";
    public static final String COL_6 = "PICTURE";


    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db = this.getWritableDatabase();
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE "  + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, CITY TEXT, LOCATION TEXT, CATEGORY TEXT, PICTURE BLOB)");
        // db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY, NAME TEXT,CITY TEXT,LOCATION TEXT, CATEGORY TEXT, PICTURE BLOB)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(sqLiteDatabase);
    }




    public boolean insertData(String name, String city, String location,String category, byte [] image ){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();

        contentValues.put(COL_2,name);
        contentValues.put(COL_3,city);
        contentValues.put(COL_4,location);
        contentValues.put(COL_5,category);
        contentValues.put(COL_6, image);
        long result = db.insert(TABLE_NAME,null,contentValues);
        if(result == -1){
            return false;
        } else{
            return true;
        }

    }

}

有什么建议吗?谢谢你!

0 个答案:

没有答案