Decode a large base64 string

I created a base64 string from an image on the SD card using this (below) code, and it works, but when I try to decode it (even lower), I get java.lang.outOfMemoryException, apparently, because I am not splitting the string into a reasonable size before I decrypt it, as I am, before I encode it.

byte fileContent[] = new byte[3000];
StringBuilder b = new StringBuilder();
try{
     FileInputStream fin = new FileInputStream(sel);
     while(fin.read(fileContent) >= 0) {
    b.append(Base64.encodeToString(fileContent, Base64.DEFAULT));
     }
}catch(IOException e){

}

The above code works well, but the problem occurs when I try to decode an image using the following code:

byte[] imageAsBytes = Base64.decode(img.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView)this.findViewById(R.id.ImageView);
image.setImageBitmap(
    BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);

I tried this way too

byte[] b = Base64.decode(img, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
image.setImageBitmap(bitmap);

Now I assume that I need to split the string into sections, such as image encoding code, but I don't know how to do this.

+5
source share
3 answers

, AsyncTask BitmapFactory. :

 BitmapFactory.Options options = new BitmapFactory.Options();

            options.inSampleSize = 2;
            options.inPurgeable=true;
        Bitmap bm = BitmapFactory.decodeFile("Your image exact loaction",options);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object 

        byte[] b = baos.toByteArray(); 
        String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
+13

  • base64 . 3 4 ( 8/3)

.

, decodeByteArray, decodeFile

+1

.

base64, 6 , 6x4 = 24 = 3 4 . , 4 base64, 3 . , base64, , 4.

+1

All Articles