繁体   English   中英

Android:从位图转换为Byte [] OutOfMemory错误

[英]Android: conversion from bitmap to Byte[] OutOfMemory error

我尝试将保存在手机存储中的图像(.jpg)转换为位图,然后将此位图转换为byte [],以将该图像上传到数据库中。

public void insertData() {
        File file = new File(photoUrl);
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        bytes = stream.toByteArray();
        DBConnUpdate conn = new DBConnUpdate();
        conn.execute(   "insert into test (img, name) "+
                        "values ( '"+ bytes +"', '"+ productName +"')");

    }

但由于这里出现OutOfMemory错误而无法正常工作:

bytes = stream.toByteArray();

为什么?

Try This Code:-


 BitmapFactory.Options options = new BitmapFactory.Options();
                 options.inDither = false;
                 options.inJustDecodeBounds = false;
                 options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                 options.inSampleSize = 1;
                 options.inPurgeable = true;
                 options.inPreferQualityOverSpeed = true;
                 options.inTempStorage=new byte[32 * 1024];

    File file = new File(photoUrl);
            Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
            bytes = stream.toByteArray();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM