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

Android 是否可以使用BitmapFactory.decodeFile方法从http位置解码图像?,android,http,bitmap,Android,Http,Bitmap,我想知道是否可以使用BitmapFactory.decodeFile方法从http位置解码图像 例如 ImageView imageview = new ImageView(context); Bitmap bmp = BitmapFactory.decodeFile("http://<my IP>/test/abc.jpg"); imageview.setImageBitmap(bmp); ImageView=newimageview(上下文); 位图bmp=BitmapFa

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

例如

ImageView imageview = new ImageView(context);
Bitmap bmp = BitmapFactory.decodeFile("http://<my IP>/test/abc.jpg");  
imageview.setImageBitmap(bmp);
ImageView=newimageview(上下文);
位图bmp=BitmapFactory.decodeFile(“http:///test/abc.jpg");  
设置图像位图(bmp);
但是bmp总是返回null

有没有其他方法来实现这个场景,在我的服务器PC中有一组图像,并且通过xml将图像加载到我的gallery应用程序中

谢谢,

Sen

使用decodeStream并传递URL的inputstream

以下是一个例子:

Bitmap bmp = BitmapFactory.decodeStream(new java.net.URL(url).openStream())
创建类名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;
      }
}
公共类SanInputStream扩展了FilterInputStream{
公共输入流(输入流输入){
超级(in),;
}
公共长跳过(长n)引发IOException{
长m=0L;
而(m
@Amir&@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) {
}
ImageView iv=新的ImageView(上下文);
试一试{
字符串url1=”http:///test/abc.jpg";
URL ulrn=新URL(url1);
HttpURLConnection con=(HttpURLConnection)ulrn.openConnection();
InputStream=con.getInputStream();
位图bmp=BitmapFactory.decodeStream(is);
如果(null!=bmp)
iv.设置图像位图(bmp);
其他的
System.out.println(“位图为空”);
}捕获(例外e){
}
谢谢,

Sen

如果我没有弄错,@Sen代码片段在.BMP文件中应该返回null,而logcat应该记录:

skia decoder->decode returned false
如果发生类似情况,请尝试使用此代码(在位图输入的情况下也适用):


//为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;
    }
}
私有类GetXMLTask扩展了AsyncTask{
@凌驾
受保护位图doInBackground(字符串…URL){
位图映射=空;
for(字符串url:url){
map=下载图像(url);
}
返回图;
}
//设置doInBackground返回的位图
@凌驾
受保护的void onPostExecute(位图结果){
设置图像位图(结果);
}
//从InputStream创建位图并返回它
私有位图下载图像(字符串url){
位图=空;
InputStream=null;
BitmapFactory.Options bmOptions=新的BitmapFactory.Options();
bmOptions.inSampleSize=1;
试一试{
流=getHttpConnection(url);
位图=位图工厂。
解码流(流,空,bmOptions);
stream.close();
}捕获(IOE1异常){
e1.printStackTrace();
}
返回位图;
}
//建立HttpURLConnection并返回InputStream
私有InputStream getHttpConnection(字符串urlString)
抛出IOException{
InputStream=null;
URL=新URL(URL字符串);
URLConnection=url.openConnection();
试一试{
HttpURLConnection httpConnection=(HttpURLConnection)连接;
setRequestMethod(“GET”);
httpConnection.connect();
if(httpConnection.getResponseCode()==HttpURLConnection.HTTP\u确定){
stream=httpConnection.getInputStream();
}
}捕获(例外情况除外){
例如printStackTrace();
}
回流;
}
}

您的意思是:位图bmp=BitmapFactory.decodeStream(“http:///test/abc.jpg"); ?如何传递URL的输入流??我希望,你得到的Skia解码器返回错误,确保你是否产生此消息,检查你的日志猫,对于此消息???@Sankar:ya,我检查了日志猫,我不是“得到Skia解码器返回错误”消息。然后告诉我什么消息,你的Logcat中有吗?@Sankar:我正试图将图像传递给画廊,因此上述步骤将在我的Avtivity的getView方法中实现。我得到的只是bmp在所有位置都为null。SANIPUTStream的用途是什么?@Amir Raminfar:BitmapFactory.decodeStream()方法无法读取JPEG图像(即返回null),如果所用FilterInputStream的skip()方法跳过的字节少于所需的字节数。就是说,我在我的类sanInputStream中使用了FilterInputStream的skip方法,但从来没有遇到过这个问题。我正在阅读PNG,JPEG和其他一切都很好。这是1.6+版本中的一个bug吗?不要忘记断开连接(从而释放输入流资源),例如,
finally{if(con!=null){con.disconnect();}}
Ref:
task.execute(new String[] { "ImageURL" });
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;
    }
}