来自 URL 的 Android 位图始终为空 [英] Android Bitmap from URL always null

查看:35
本文介绍了来自 URL 的 Android 位图始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 URL 下载图像并将其转换为位图,但是该行

I'm trying to download an image from a URL and convert it into a bitmap, but the line

Bitmap myBitmap = BitmapFactory.decodeStream(input);

总是让调试器跳到下一行

always causes the debugger to skip to the following line

return null;

没有实际打印出堆栈跟踪,并且调试器中列出的变量中也不存在 Exception 变量.我阅读了很多关于 url 可能存在的问题,实际上并没有导致图像、格式不正确的图像等问题,但它仍然存在与我肯定存在的硬编码图像相同的问题.

without ever actually printing out the stack trace and the Exception variable also doesn't exist in the variables listed in the Debugger. I read a lot about how there might be issues with urls not actually leading to images, not well formated images and the like but it still has the same issue with a hardcoded image that I'm positive exists.

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(
                "http://www.helpinghomelesscats.com/images/cat1.jpg");
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

因为似乎可能涉及 manifest.xml 文件,所以我已编辑添加到此处.

Since it seems like the manifest.xml file might be involved I've edited to add here.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.delivery"
android:versionCode="1"
android:versionName="1.0">

<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".IntroPage"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Browse"></activity>
    <activity android:name=".ViewProduct"></activity>
    <activity android:name=".ViewOrder"></activity>
    <activity android:name=".GetAddress"></activity>
    <activity android:name=".ConfirmOrder"></activity>
</application>

推荐答案

以下代码适用于所有类型的图像.

following code work for all type of images.

    try {

        URL url = new URL("http://www.helpinghomelesscats.com/images/cat1.jpg");
        InputStream in = url.openConnection().getInputStream(); 
        BufferedInputStream bis = new BufferedInputStream(in,1024*8);
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int len=0;
        byte[] buffer = new byte[1024];
        while((len = bis.read(buffer)) != -1){
            out.write(buffer, 0, len);
        }
        out.close();
        bis.close();

        byte[] data = out.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        imageView.setImageBitmap(bitmap);
    }
    catch (IOException e) {
        e.printStackTrace();
    }

这篇关于来自 URL 的 Android 位图始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆