android studio中数据库中的图像插入

时间:2017-10-17 00:56:01

标签: android mysql

我通过捕获相机或从库中选择然后尝试通过按钮单击发送另一个活动来选择图像我试图在数据库中插入图像base64值。但实际值没有插入,请帮我解决问题。即使我想将图像发送到另一个活动,活动也会停止工作。代码如下 -

ib1 = (ImageButton) findViewById(R.id.imageButton);
        ib1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openGallery();
            }
        });
        ib2 = (ImageButton) findViewById(R.id.imageButton2) ;
        ib2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cameraCapture();
            }
        });

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*im.setDrawingCacheEnabled(true);
                im.buildDrawingCache();
                Bitmap bitmap = im.getDrawingCache(); */
                /*Drawable drawable = im.getDrawable();
                BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
                Bitmap bitmap = bitmapDrawable.getBitmap(); */


              /*  Bitmap bitmap = ((BitmapDrawable) im.getDrawable()).getBitmap();


                byte[] pic =imageToString(bitmap); */



                if (pic.toString().matches("") || e1.getText().toString().matches("") ||
                        e2.getText().toString().matches("") || e3.getText().toString().matches("") || e4.getText().toString().matches("")
                        ||
                        b2.getText().toString().matches("") || b3.getText().toString().matches("")) {
                    Toast.makeText(MainActivity.this, "Please! fill all the field", Toast.LENGTH_SHORT).show();
                    return;
                } else {

                    // startActivity(new Intent(MainActivity.this,EventAdding.class));
                    Intent next = new Intent(MainActivity.this, EventList.class);


                    Bundle bn = new Bundle();
                    next.putExtra("image",pic);
                    bn.putString("event_name", e1.getText().toString());
                    bn.putString("date", b2.getText().toString());
                    bn.putString("time", b3.getText().toString());
                    bn.putString("event_address", e3.getText().toString());
                    bn.putString("email_address", e4.getText().toString());
                    bn.putString("tel_no", e2.getText().toString());
                    next.putExtras(bn);
                    startActivity(next);
                }
            }

        });



    }



    public void setDate(View v) {


        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthofyear, int dayofmonth) {
                b2.setText(dayofmonth + "-" + monthofyear + "-" + year);
            }
        }, year, month, day);
        dpd.show();
    }


    public void setTime(View v) {
        final Calendar c = Calendar.getInstance(Locale.getDefault());
        hour = c.get(Calendar.HOUR_OF_DAY);
        min = c.get(Calendar.MINUTE);

        TimePickerDialog tpd = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourofday, int minute) {
                b3.setText(hourofday + ":" + minute);
            }
        }, hour, min, false);
        tpd.show();

    }

    private void cameraCapture(){
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }

    private static final int PICK_IMAGE = 150;
    Uri imageUrl;

    private void openGallery() {

        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(intent, PICK_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestcode, int resultcode, Intent data) {
        super.onActivityResult(requestcode, resultcode, data);
        if (resultcode == RESULT_OK && requestcode == PICK_IMAGE ) {
            imageUrl = data.getData();
            try {

                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUrl );
                // Log.d(TAG, String.valueOf(bitmap));

                im.setImageBitmap(bitmap);

            } catch (IOException e) {
                e.printStackTrace();
            }
            im.setImageURI(imageUrl);
        }


        if (requestcode == CAMERA_REQUEST && resultcode == RESULT_OK) {
            Bitmap photo;

            photo = (Bitmap) data.getExtras().get("data");
            im.setImageBitmap(photo);//set image to Imageview


        }
    }


    private String imageToString(Bitmap photo) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        return encodedImage;
}

在第二项活动中 -

 Bundle bundle = getIntent().getExtras();
    img = bundle.getString("image");

2 个答案:

答案 0 :(得分:0)

在第二个活动中,您尝试使用此行

从包中获取字节数组
byteArray = bundle.getByteArray("picture");

但是你没有在第一个活动中将字节数组附加到bundle。

答案 1 :(得分:0)

你必须在数据库中将图像作为blob插入,

byteArray=getBayteArray(image);

然后将其插入到具有blob类型列的数据库中。 这就是你得到字节数组的方法

 public static byte[] getBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}