请裸露我的英语

如何将捕获的图像上传到数据库?

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );

    camera = (ImageButton) findViewById( R.id.camera );

    camera.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog = new Dialog( MainActivity.this );
            dialog.setContentView( R.layout.camera_gallery );
            dialog.setTitle( "Upload Image" );
            dialog.setCancelable( true );
            dialog.show();



            capture = (Button) dialog.findViewById( R.id.capture );
            download = (Button) dialog.findViewById( R.id.download );


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

                    Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
                    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    getFileUri();
                    i.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
                    startActivityForResult(i, 10);
                }
            } );

            download.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent( Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
                    intent.setType("image/*");
                    startActivityForResult( intent, gallery );
                }
            } );

        }
    } );

}


private void getFileUri() {
    image_name = "testing123.jpg";
    file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            + File.separator + image_name
    );

    file_uri = Uri.fromFile(file);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

                    //code here
    }
}


在这里,我想将捕获的图像也以原始大小(而不是缩略图)添加到数据库中。

我还有一个关于MySQL的知识。我们可以将图像文件存储在数据库中吗?如果没有,它们如何存储图像。

还有关于如何将捕获的图像存储在内部存储器中的任何建议。

最佳答案

您可以将图像转换为base64格式。例如:

    bitmap2 = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), uri);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String ImageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);

09-10 13:22
查看更多