小编Swa*_*and的帖子

尽管存在图像,但BitmapFactory返回null

在这里,我想从字符串URL转换图像。尽管有一个包含图像的URL,但它返回null。我在下面共享了代码。

private byte[] convertImageToByteArray(String imgPath)
{

    byte[] byteArray = null;
    Bitmap bmp = BitmapFactory.decodeFile(imgPath);
    if(bmp != null)
    {

        try {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            //bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byteArray = stream.toByteArray();

            try 
            {
                stream.close();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();

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

        }
    }
    else
    {
        try {
            Bitmap bmpDefault = BitmapFactory.decodeResource(getResources(), R.drawable.na);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            //bmpDefault.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            bmpDefault.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byteArray = stream.toByteArray();
        } 
        catch (Exception e) 
        { …
Run Code Online (Sandbox Code Playgroud)

android bitmapfactory

5
推荐指数
1
解决办法
294
查看次数

标签 统计

android ×1

bitmapfactory ×1