Android调整大图像的大小已收到base64字符串

Geo*_*rge 2 base64 android image bitmapfactory

我有一些问题在android中调整图像大小.我有一个base64字符串(我没有文件或网址,只有字符串),到目前为止,我尝试解码它时,我得到一个内存不足的异常.

public String resizeBase64Image(String base64image){

    byte [] encodeByte=Base64.decode(base64image,Base64.DEFAULT); //out of memory exception...

    BitmapFactory.Options options=new BitmapFactory.Options();
    options.inPurgeable = true;
    Bitmap image = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length,options);

    image = Bitmap.createScaledBitmap(image, IMG_WIDTH, IMG_HEIGHT, false);

    ByteArrayOutputStream baos=new  ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG,100, baos);
    byte [] newbytes=baos.toByteArray();

    return Base64.encodeToString(newbytes, Base64.DEFAULT);

}
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

Geo*_*rge 5

我花了一段时间才回到这个项目,但我终于找到了解决问题的方法.首先,我需要修改Manfest的应用程序标签以添加largeHeap =:true":

`<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:largeHeap="true" >`
Run Code Online (Sandbox Code Playgroud)

利用垃圾收集器帮了很多忙.

public String resizeBase64Image(String base64image){
    byte [] encodeByte=Base64.decode(base64image.getBytes(),Base64.DEFAULT); 
    BitmapFactory.Options options=new BitmapFactory.Options();
    options.inPurgeable = true;
    Bitmap image = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length,options);


    if(image.getHeight() <= 400 && image.getWidth() <= 400){
        return base64image;
    }
    image = Bitmap.createScaledBitmap(image, IMG_WIDTH, IMG_HEIGHT, false);

    ByteArrayOutputStream baos=new  ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG,100, baos);

    byte [] b=baos.toByteArray();
    System.gc();
    return Base64.encodeToString(b, Base64.NO_WRAP);

}   
Run Code Online (Sandbox Code Playgroud)