是否可以使用 BitmapFactory.decodeFile 方法从 http 位置解码图像?

发布于 2024-10-08 22:31:09 字数 363 浏览 3 评论 0原文

我想知道是否可以使用 BitmapFactory.decodeFile 方法从 http 位置解码图像?

例如。

ImageView imageview = new ImageView(context);
Bitmap bmp = BitmapFactory.decodeFile("http://<my IP>/test/abc.jpg");  
imageview.setImageBitmap(bmp);

但 bmp 总是返回 null。

有没有其他方法可以实现这种情况,即我的服务器 PC 中有一组图像,并且我通过 xml 将图像加载到我的图库应用程序中?

谢谢,

I would like to know if it is possible to use BitmapFactory.decodeFile method to decode a image from http location?

For eg.

ImageView imageview = new ImageView(context);
Bitmap bmp = BitmapFactory.decodeFile("http://<my IP>/test/abc.jpg");  
imageview.setImageBitmap(bmp);

But bmp is always returning null.

Is there any other way to achieve this scenario, where i have a set of images in my server PC, and i am loading the images to my gallery application via an xml?

Thanks,
Sen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

分开我的手 2024-10-15 22:31:10

使用decodeStream 并传递URL 的输入流。

这是一个例子:

Bitmap bmp = BitmapFactory.decodeStream(new java.net.URL(url).openStream())

Use decodeStream and pass the URL's inputstream instead.

Here is an example:

Bitmap bmp = BitmapFactory.decodeStream(new java.net.URL(url).openStream())
许久 2024-10-15 22:31:10

@阿米尔& @Sankar:感谢您的宝贵建议。

我通过执行以下代码片段解决了上述问题:

ImageView iv = new ImageView(context);

try{
    String url1 = "http://<my IP>/test/abc.jpg";
    URL ulrn = new URL(url1);
    HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
    InputStream is = con.getInputStream();
    Bitmap bmp = BitmapFactory.decodeStream(is);
    if (null != bmp)
        iv.setImageBitmap(bmp);
    else
        System.out.println("The Bitmap is NULL");

} catch(Exception e) {
}

谢谢,

@Amir & @Sankar : Thanks for your valuable suggestions.

I solved the above problem by doing the following code snippet :

ImageView iv = new ImageView(context);

try{
    String url1 = "http://<my IP>/test/abc.jpg";
    URL ulrn = new URL(url1);
    HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
    InputStream is = con.getInputStream();
    Bitmap bmp = BitmapFactory.decodeStream(is);
    if (null != bmp)
        iv.setImageBitmap(bmp);
    else
        System.out.println("The Bitmap is NULL");

} catch(Exception e) {
}

Thanks,
Sen

捶死心动 2024-10-15 22:31:10
String urldisplay="http://www.google.com/";//sample url
Log.d("url_dispaly",urldisplay);
try{    
InputStream in = new java.net.URL(urldisplay).openStream();
Bitmap  mIcon11 = BitmapFactory.decodeStream(new SanInputStream(in));
}
catch(Exception e){}

创建类名SanInputStream

public class SanInputStream extends FilterInputStream {
      public SanInputStream(InputStream in) {
        super(in);
      }
      public long skip(long n) throws IOException {
        long m = 0L;
        while (m < n) {
          long _m = in.skip(n-m);
          if (_m == 0L) break;
          m += _m;
        }

        return m;
      }
}
String urldisplay="http://www.google.com/";//sample url
Log.d("url_dispaly",urldisplay);
try{    
InputStream in = new java.net.URL(urldisplay).openStream();
Bitmap  mIcon11 = BitmapFactory.decodeStream(new SanInputStream(in));
}
catch(Exception e){}

Create class name SanInputStream

public class SanInputStream extends FilterInputStream {
      public SanInputStream(InputStream in) {
        super(in);
      }
      public long skip(long n) throws IOException {
        long m = 0L;
        while (m < n) {
          long _m = in.skip(n-m);
          if (_m == 0L) break;
          m += _m;
        }

        return m;
      }
}
泛泛之交 2024-10-15 22:31:10

如果我没有记错的话,@Sen 代码片段应该在 .BMP 文件的情况下返回 null,并且 logcat 应该记录:

skia decoder->decode returned false

如果发生类似的情况,请尝试使用此代码(也适用于位图输入):

HttpGet httpRequest = null;

try {
    httpRequest = new HttpGet(url.toURI());
} catch (URISyntaxException e) {
    e.printStackTrace();
}

HttpClient httpclient = new DefaultHttpClient();

HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

HttpEntity entity = response.getEntity();

BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);

InputStream instream = bufHttpEntity.getContent();

bmp = BitmapFactory.decodeStream(instream);

来源

If I'm not mistaken, @Sen code snippet should return null in case of .BMP file and logcat should log:

skia decoder->decode returned false

If something like this is happening, try using this code (works also in case of bitmap input):

HttpGet httpRequest = null;

try {
    httpRequest = new HttpGet(url.toURI());
} catch (URISyntaxException e) {
    e.printStackTrace();
}

HttpClient httpclient = new DefaultHttpClient();

HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

HttpEntity entity = response.getEntity();

BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);

InputStream instream = bufHttpEntity.getContent();

bmp = BitmapFactory.decodeStream(instream);

Source

甲如呢乙后呢 2024-10-15 22:31:10

// 为 AsyncTask 的子类创建一个对象

GetXMLTask task = new GetXMLTask();

// 执行任务

task.execute(new String[] { "ImageURL" });

// 然后在 Asyntask 类中将图像分配给图像视图以避免 android.os.NetworkOnMainThreadException

private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap map = null;
        for (String url : urls) {
            map = downloadImage(url);
        }
        return map;
    }

    // Sets the Bitmap returned by doInBackground
    @Override
    protected void onPostExecute(Bitmap result) {
        imageView.setImageBitmap(result);
    }

    // Creates Bitmap from InputStream and returns it
    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.
                    decodeStream(stream, null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString)
            throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }
}

// Create an object for subclass of AsyncTask

GetXMLTask task = new GetXMLTask();

// Execute the task

task.execute(new String[] { "ImageURL" });

//then in Asyntask class assign the image to image view to avoid android.os.NetworkOnMainThreadException

private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap map = null;
        for (String url : urls) {
            map = downloadImage(url);
        }
        return map;
    }

    // Sets the Bitmap returned by doInBackground
    @Override
    protected void onPostExecute(Bitmap result) {
        imageView.setImageBitmap(result);
    }

    // Creates Bitmap from InputStream and returns it
    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.
                    decodeStream(stream, null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString)
            throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文