穆罕默德 - Moh
穆罕默德 - Moh

Reputation: 1373

convert to base 64 with A reapeat in result?

I get url in android and convert data stream to 64 bit data string with this code:

URL url = new URL("http://iranassistance.com/images/sos-logo.png");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
//urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile();
String filename="downloadedFile.png";
Log.i("Local filename:",""+filename);
File file = new File(SDCardRoot,filename);
if(file.createNewFile()) {
  file.createNewFile();
}
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();byte[] imageBytes = new byte[urlConnection.getContentLength()];
inputStream.read(imageBytes, 0, imageBytes.length);
inputStream.close();
String base64Image = Base64.encodeToString(imageBytes, Base64.DEFAULT);

But the base64Image result is not complete and gave something like this :

......nUTJaJnb7PLyscfBMQLLiexyKSEh/o2RfctcZtc8Hr5xcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA........

The repeated 'A' show something is wrong and image is not complete! Why this not work properly ?

Upvotes: 1

Views: 54

Answers (3)

穆罕默德 - Moh
穆罕默德 - Moh

Reputation: 1373

GhostCat said the correct answer , i change my code as bellow and it worked find :

 InputStream is = null;
    try {

        URL url = new URL("http://iranassistance.com/images/sos-logo.png");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        is = url.openStream ();
        byte[] byteChunk = new byte[4096]; 
        int n;

        while ( (n = is.read(byteChunk)) > 0 ) {
            baos.write(byteChunk, 0, n);
        }

        String base64Image2 = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
        db.UpdateImage64(base64Image2);
        productModel pd = db.GetProductById(2);

    }
    catch (IOException e) {
        e.printStackTrace ();

    }
    finally {
        if (is != null) {
            try{
                is.close();
            }
                catch (IOException s){

                }
        }
    }

Upvotes: 0

GhostCat
GhostCat

Reputation: 140417

Simple:

inputStream.read(imageBytes, 0, imageBytes.length);

You assume that the above always reads all bytes in one shot.

Wrong. This method reads as many bytes as it wants to read. Therefore it is returning you the number of bytes read. See its javadoc:

Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

In other words: you have to loop and accumulate these numbers until you got exactly the amount of bytes you are looking for!

And you get those A chars: your array is initially all 0. As explained: you are only filling parts of that array. So the rest of the arrays has still 0s in it - which results as AAAAAs after encoding.

Upvotes: 1

Nikhil Rai
Nikhil Rai

Reputation: 61

You can use the following function to convert image into base64 just pass on your image....

private String encodeImage(Bitmap mphoto)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    mphoto.compress(Bitmap.CompressFormat.JPEG,100,baos);
    byte[] b = baos.toByteArray();
    String encImage = Base64.encodeToString(b, Base64.DEFAULT);

    return encImage;
}

Upvotes: 0

Related Questions