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.

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

So in my database i have to put the ImageString as a input?
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.
And how to convert it in Image file again. And can you help it out by editing it in my give code
it depend. if you using from php you can use from this: stackoverflow.com/questions/15153776/…. OR : stackoverflow.com/questions/11511511/… in Server side

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.