Не удается загрузить изображение в Android - PullRequest
0 голосов
/ 01 марта 2012

Я всегда получаю отклик NULL при вызове этого метода, но в случае, если предположим, что я запускаю в браузере, он отображает изображение. Public Bitmap getBitmapFromURL (String src) { Растровое изображение bmImg; URL myFileUrl = null;

    try {
        myFileUrl = new URL("http://www.russiawear.com/components/com_virtuemart/shop_image/product/youth_russia_usa_4e2f7f78b543c.jpg");

        HttpURLConnection conn = (HttpURLConnection) myFileUrl
                .openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();

        BitmapFactory.Options options = new BitmapFactory.Options();

        bmImg = BitmapFactory.decodeStream(is, null, options);
        return bmImg;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}`

в чем может быть проблема?

Ответы [ 2 ]

0 голосов
/ 09 мая 2012
private Bitmap loadBitmap() {
    Bitmap bmQR = null;
    InputStream inputStream = null;

    try {
        inputStream = OpenHttpConnection(url);
        bmQR = BitmapFactory.decodeStream(inputStream);
        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bmQR;
}

private InputStream OpenHttpConnection(String strURL) throws IOException {
    InputStream is = null;
    URL url = new URL(strURL);
    URLConnection urlConnection = url.openConnection();

    try {
        HttpURLConnection httpConn = (HttpURLConnection) urlConnection;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            is = httpConn.getInputStream();
        }
    } catch (Exception ex) {
    }
    return is;
}
0 голосов
/ 01 марта 2012

Я просто использую это, и в моем случае это прекрасно работает.

URL newurl;
try
{
newurl = new URL("http://www.russiawear.com/components/com_virtuemart/shop_image/product/youth_russia_usa_4e2f7f78b543c.jpg");  
bitmap = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

Обновление:

Код для скачивания файла с URL,

    try
    {
        URL url = new URL("http://www.russiawear.com/components/com_virtuemart/shop_image/product/youth_russia_usa_4e2f7f78b543c.jpg"); //you can write here any link
        File file = new File("/sdcard/temp.jpg");
        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();

        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1)
        {
            baf.append((byte) current);
        }

        /* Convert the Bytes read to a String. */
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.close();
    }
    catch (IOException e)
    {
        Log.d("ImageManager", "Error: " + e);
    }

Затем создайте растровое изображение, используя файл,

bitmap = BitmapFactory.decodeFile("/sdcard/temp.jpg");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...