Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to upload image in base64 on server

i have a problem, i am uploading image on server but it is not. i have convert image in base64 and get through json. but json is not properly closed due to this i m getting error. error id om postimafe variable. in this variable {"key"""encode, here is json is not closed.

        // code for convert base64

        public static String getBase64String(String baseFileUri)
            {
                String encodedImageData  = "";
                try
                {
                    System.out.println("getBase64String method is called :" +baseFileUri);
                    Bitmap bm = BitmapFactory.decodeFile(baseFileUri);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
                    byte[] b = baos.toByteArray();
                    encodedImageData  = Base64.encodeToString(b, Base64.DEFAULT);
                    //ArrayList<NameValuePair> imagearraylistvalue = new ArrayList<NameValuePair>();
                    //imagearraylistvalue.add(new BasicNameValuePair("image", encodedImage));

                    System.out.println("encode data in upload file :" +encodedImageData );
                }
                catch(Exception ex)
                {
                    System.out.println("Exception in getBase64String method in Utility class :" +ex);
                }
                return encodedImageData ;
            }

        // code for json and uplod base64 to server but i m getting error

        System.out.println("fullupload image for 1:" +fulluploadimgpath);
    String base64String = Utility.getBase64String(fulluploadimgpath);
    System.out.println("base64String is in :" +base64String);
    if (base64String != null) 
{
JSONObject postImageData = new JSONObject();

postImageData.put("media",base64String);

 System.out.println("post image :" +postImageData);
HttpResponse imgPostResponse = Utility.postDataOnUrl(Utility.getBaseUrl()+"user/upload",obj.toString());
System.out.println("fullupload image for imgPostResponse:" +imgPostResponse);

     if (imgPostResponse != null)
 {

String imgResponse = Utility.readUrlResponseAsString(imgPostResponse);
System.out.println("imgResponse is in imgResponse :" +imgResponse);
if (imgResponse != null|| imgResponse.trim().length() != 0)
                                                    {
                                                        JSONObject jResObj = new JSONObject();
                                                            if (jResObj.getBoolean("rc"))
                                                            {
                                                            obj.put(hidobj.getReceiveAs(),jResObj.getLong("ident"));
                                                        }

}
like image 902
Amit Kumar Avatar asked Sep 30 '14 07:09

Amit Kumar


People also ask

How do I make an image a Base64 URL?

One way to convert an image file into a base64 string is to put it into a canvas. Then we can call the canvas's toDataURL method to convert it into a base64 string. We create the getBase64Image function that takes the url string for the URL of the image. Then we create an img eklement with the Image constructor.

How do I display an image in Base64 tag?

Use the img Tag to Display Base64 Image in HTML We can specify the URL of the image in the src attribute of the img tag. We can display base64 images by providing the information about the image in the src attribute. We need to define the accurate content type, content-encoding, and charset in the src attribute.


1 Answers

String encodedImageData =getEncoded64ImageStringFromBitmap(your bitmap);

public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70, stream);
    byte[] byteFormat = stream.toByteArray();
    // get the base 64 string
    String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);

    return imgString;
}
like image 153
Vaishali Sutariya Avatar answered Nov 25 '22 05:11

Vaishali Sutariya