从URL总是空的Andr​​oid位图位图、URL、Andr、oid

由网友(做个废柴继续可爱)分享简介:我想从URL下载图像,并将其转换成位图,但行I'm trying to download an image from a URL and convert it into a bitmap, but the lineBitmap myBitmap = BitmapFactory.decodeStream(input)...

我想从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;

,却从来没有打印出堆栈跟踪和异常变量也不会在调试器中列出的变量存在。我读了很多关于如何有可能是使用URL实际上并不导致图像,没有得到很好的格式化图像等问题,但它仍然与我敢肯定存在一个硬codeD映像相同的问题。

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(
                "https://p.xsw88.cn/allimgs/daicuo/20230907/4183.png.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>

推荐答案

以下为所有类型的图像code的工作。

following code work for all type of images.

    try {

        URL url = new URL("https://p.xsw88.cn/allimgs/daicuo/20230907/4183.png.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();
    }
阅读全文

相关推荐

最新文章