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());
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.
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!
Bitmap bm=((BitmapDrawable)imageView. getDrawable()). getBitmap(); Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)
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); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With