This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to use BitmapFactory.decodeFile method to decode a image from http location?
i have a problem in set url image to image view. i tried below methods
Method 1:
Bitmap bimage= getBitmapFromURL(bannerpath);
image.setImageBitmap(bimage);
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
Method 2:
Drawable drawable = LoadImageFromWebOperations(bannerpath);
image.setImageDrawable(drawable);
private Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
i tried above two methods but are not working . both are showing DEBUG/skia(266): --- decoder->decode returned false . i used demo url image paths that is working. but this path is not working .so please tell me what is the wrong and what i will do
Thank you in advance.
Best Regards.
Just Tested your "Method 1" in my app and it works just fine.
You might have forgotten this line in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
Related
I want to set layout background image from web URL,
either using volley or Picasso or some else
is there any way to set this!
I found below code this from web but not working !!
Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");
RelativeLayout rLayout=(RelativeLayout)findViewById(R.id.relativeLayout);
//BitmapDrawable(obj) convert Bitmap object into drawable object.
Drawable dr = new BitmapDrawable(myImage);
rLayout.setBackgroundDrawable(dr);
public Bitmap getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
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;
}
}
Please try this Android Smart Image View library.
http://loopj.com/android-smart-image-view/
I use the below link for set image from url:
https://android-arsenal.com/details/1/211
If you are using android studio then put compile 'com.squareup.picasso:picasso:2.5.2' in your build.gradle file and compile the project.
And then you can use Picasso library
Picasso.with(mContext)
.load("http://looksok.files.wordpress.com/2011/12/me.jpg")
.into(rLayout);
This will load image from given url into rLayout.
Reference : http://square.github.io/picasso/
This question already has answers here:
Android load from URL to Bitmap
(20 answers)
Closed 7 years ago.
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 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.
Okay so you are trying to get a bitmap from a file? Title says URL. Anyways, when you are getting files from external storage in Android you should never use a direct path. Instead call getExternalStorageDirectory() like so:
File bitmapFile = new File(Environment.getExternalStorageDirectory() + "/" + PATH_TO_IMAGE);
Bitmap bitmap = BitmapFactory.decodeFile(bitmapFile);
getExternalStorageDirectory() gives you the path to the SD card.
Also you need to declare the WRITE_EXTERNAL_STORAGE permission in the Manifest.
i am getting bitamap from a url. Everything is fine but i m having small issue i.e my bitmap from url gets scales down. I want bitmap of original size without scaling. How to achieve this. I tried options but no success yet. here is my code:
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;
}
I set this drawable to imagview. But image is scaled down. I want original size and original size is not too big so need to worry about outofmemory exception. Is there any way to avoid scaling while parsing bitmap from url stream.
Thanks.
Directly show image from web without downloading it. Please check the below function . It will show the images from the web into your image view.
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
then set image to imageview using code in your activity. Maybe this will help you.
I am making an application in android which loads Icon size images from URL
i have tried downloading images using the following code.
One image labeled default.png was downloaded from the given url but there was another image labeled v_1234.jpg is not being downloaded. I dont know whats the problem. it just returns me null for jpg image.
I am not sure that its a problem for .jpg format that my code is not downloading the jpg format images or Its the labeled name problem that due to Underscore (_) in the label makes it not downloadable..
Please help Friends you are professional in that field.
CODE:
URL url = new URL(detail.voucher_image.toString());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.getImageBitmap(bmp);
Thanks alot.
try this code
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
InputStream reader;
reader=conn.getInputStream();
System.out.println("Compressed2!!!"+conn.getContentLength());
int available = reader.available();
int i=0;
int count=0;
int cc=0;
while(reader.read()!=-1){
cc++;
}
System.out.println("available"+cc);
data2 = new byte[cc];
while ((i = reader.read(data2, count, data2.length-count)) != -1) {
count +=i;
cc++;
}
System.out.println("Compressed3!!!");
// reader.read(data2,0,cc);
System.out.println("Compressed!!!");
// printBytes(data1,data2,"after");
System.out.println("length b4!!!"+data2);
System.out.println("data::"+new String(data2));
System.out.println("The length is "+data2.length);
bmp2=BitmapFactory.decodeByteArray(data2, 0, data2.length);
if(bmp2==null)
System.out.println("The bitmap value is null");
iv.setImageBitmap(bmp2);undefined
use the following code to get bitmap from url
public Bitmap imageConvert(String url){
URL aURL = null;
Bitmap bm = null;
try {
final String imageUrl =imgstr.replaceAll(" ","%20");
Log.e("Image Url",imageUrl);
aURL = new URL(imageUrl);
URLConnection conn = aURL.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(new PatchInputStream(is));
is.close();
}
catch (Exception e) {
Log.e("ProPic Exception",e.getMessage());
}
return bm;
}
I want to display external image like:
"http://abc.com/image.jpg"
in my android phone application.
can any one guide me how to achieve this?
There are many ways to achieve your request. Basically you have to download the image with an urlrequest and then using the InputStream to create a Bitmap object.
Just a sample code:
URL url = new URL("http://asd.jpg");
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
After you obtain the Bitmap object you can use it on your ImageView for instance
Just another approach to download the image from a url
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://abc.com/image.jpg").getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}