1

Please bare my English

How can I upload the captured image in the database?

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

Here I want to Add the captured image into my database that too in original size, not the thumbnail.

And I have one more regarding MySQL. Can we store the Image file in the database and if not then how do they store images.

And Also any suggestion to how to store my captured image in internal storage.

Jaymin
  • 2,879
  • 3
  • 19
  • 35
Nikhil Lohar
  • 127
  • 1
  • 9

1 Answers1

0

you can convert image to base64 format. for example:

    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);
Mehrdad
  • 1,477
  • 15
  • 17
  • So in my database i have to put the ImageString as a input? – Nikhil Lohar Oct 08 '18 at 09:18
  • yes you can. but this is better that you convert ImageString to image file again and save in your host and put URL in your database. excuse me if my english is bad. – Mehrdad Oct 09 '18 at 06:21
  • And how to convert it in Image file again. And can you help it out by editing it in my give code – Nikhil Lohar Oct 10 '18 at 04:59
  • it depend. if you using from php you can use from this: https://stackoverflow.com/questions/15153776/convert-base64-string-to-an-image-file. OR : https://stackoverflow.com/questions/11511511/how-to-save-a-png-image-server-side-from-a-base64-data-string in Server side – Mehrdad Oct 10 '18 at 06:13