Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get bitmap from a url in android? [duplicate]

Tags:

android

I have a uri like which has an image

file:///mnt/............... 

How to use this uri to get the image but it returns null, please tell me where i am wrong.

Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath()); Bitmap bitmap = BitmapFactory.decodeFile(uri.toString()); 
like image 298
Chandra Sekhar Avatar asked Aug 06 '12 15:08

Chandra Sekhar


People also ask

How do I convert a bitmap to a URL?

This example demonstrates how do I get a bitmap from Url in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I change a bitmap URL to Android?

Nothing too complicated – simply create a URL object using the “src” string (i.e. String src = “https://thinkandroid.wordpress.com”), connect to it, and use Android's BitmapFactory class to decode the input stream. The result should be your desired Bitmap object!

How do I get bitmap from imageView?

Bitmap bm=((BitmapDrawable)imageView. getDrawable()). getBitmap(); Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)


2 Answers

This is a simple one line way to do it:

    try {         URL url = new URL("http://....");         Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());     } catch(IOException e) {         System.out.println(e);     } 
like image 92
brthornbury Avatar answered Sep 19 '22 06:09

brthornbury


This should do the trick:

public static Bitmap getBitmapFromURL(String src) {     try {         URL url = new URL(src);         HttpURLConnection connection = (HttpURLConnection) url.openConnection();         connection.setDoInput(true);         connection.connect();         InputStream input = connection.getInputStream();         Bitmap myBitmap = BitmapFactory.decodeStream(input);         return myBitmap;     } catch (IOException e) {         e.printStackTrace();         return null;     } } // Author: silentnuke 

Don't forget to add the internet permission in your manifest.

like image 23
Luke Taylor Avatar answered Sep 18 '22 06:09

Luke Taylor