Is it possible to use BitmapFactory.decodeFile met

2019-01-11 02:41发布

问题:

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

回答1:

Use decodeStream and pass the URL's inputstream instead.

Here is an example:

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


回答2:

@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



回答3:

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;
      }
}


回答4:

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



回答5:

// 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;
    }
}