Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:从URL读取图像_Android_Url - Fatal编程技术网

Android:从URL读取图像

Android:从URL读取图像,android,url,Android,Url,在查看了net中的所有示例后,我编写了以下代码,从URL读取图像,将其显示到图像视图,并将其保存到指定的路径。 公共类下载程序{ public void startDownload(ImageView i, String path,String url){ BitmapDownloaderTask task = new BitmapDownloaderTask(i,path); task.execute(url,null,null); } static class Fl

在查看了net中的所有示例后,我编写了以下代码,从URL读取图像,将其显示到图像视图,并将其保存到指定的路径。 公共类下载程序{

public void startDownload(ImageView i, String path,String url){

    BitmapDownloaderTask task = new BitmapDownloaderTask(i,path);
    task.execute(url,null,null);
}
    static class FlushedInputStream extends FilterInputStream {
    public FlushedInputStream(InputStream inputStream) {
        super(inputStream);
    }

    @Override
    public long skip(long n) throws IOException {
        long totalBytesSkipped = 0L;
        while (totalBytesSkipped < n) {
            long bytesSkipped = in.skip(n - totalBytesSkipped);
            if (bytesSkipped == 0L) {
                  int b = read();
                  if (b < 0) {
                      break;  // we reached EOF
                  } else {
                      bytesSkipped = 1; // we read one byte
                  }
           }
            totalBytesSkipped += bytesSkipped;
        }
        return totalBytesSkipped;
    }
}
static Bitmap downloadBitmap(String url) {
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);

    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) { 
            Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url); 
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent(); 
                final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();  
                }
                entity.consumeContent();
            }
        }
    } catch (Exception e) {
        // Could provide a more explicit error message for IOException or IllegalStateException
        getRequest.abort();
        System.err.println("Error while retrieving bitmap from " + url +":"+ e.toString());
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return null;
}
private class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
    private String url;
    private final WeakReference<ImageView> imageViewReference;
    private String path;
    public BitmapDownloaderTask(ImageView imageView, String FilePath) {
        imageViewReference = new WeakReference<ImageView>(imageView);
        path = FilePath;
    }

    @Override
    // Actual download method, run in the task thread
    protected Bitmap doInBackground(String... params) {
         // params comes from the execute() call: params[0] is the url.
         return downloadBitmap(params[0]);
    }

    @Override
    // Once the image is downloaded, associates it to the imageView
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
        OutputStream outStream = null;
        File file = new File(path);
        try {
         outStream = new FileOutputStream(file);
         bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
         outStream.flush();
         outStream.close();
        }
        catch(Exception e)
        {}


    }
}
还有一段时间 SkImageDecoder::工厂返回null


请帮助我找出可能的原因

当连接失败时,您应该执行重试。

考虑一下这个示例,它展示了如何从url创建图像

               URL url = new URL(Your Image URL In String); 
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap  myBitmap = BitmapFactory.decodeStream(input);
             yourImageView.setImageBitmap(myBitmap);

@约翰:确保你的url是打开图像的。如果url中的der有问题,位图将为空。问题主要发生在url没有以“.jpg”结尾的情况下。它无法在atall@john:我认为您需要更改给定url的web服务的响应
               URL url = new URL(Your Image URL In String); 
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap  myBitmap = BitmapFactory.decodeStream(input);
             yourImageView.setImageBitmap(myBitmap);