Don't display the Image Fetch From URL online Android - android

I want to show an image from online (url) but it shows me nothing and i don't know why this happens. I add INTERNET permission in manifest also.
private ImageView iv;
private Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String address = "http://4.bp.blogspot.com/-3_EM3_dBstc/UxD7U2EEa2I/AAAAAAAACmI/M9JilH6eIV0/s100/Krita-logo.png";
iv = (ImageView) findViewById(R.id.image);
bitmap = bitImageRead(address);
iv.setImageBitmap(bitmap);
}
Where i do wrong?

Try this..
public Bitmap bitImageRead(address){
try {
URL url = new URL(address);
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;
}
}

Related

Convert image URL to drawable resource id in android

I'm using the KenBurnsView library with this code:
mHeaderPicture.setResourceIds(R.drawable.picture0, R.drawable.picture1);
As you can see it takes drawable resource ids.
What I want to do is covert all photos URLS to drawable resource ids.
Photo urls like this:
http://example.com/image.jpg
http://example.com/image2.jpg
http://example.com/image3.jpg
http://example.com/image4.jpg
I tried this code here and it didn't work:
Bitmap drawable_from_url(String url) throws java.net.MalformedURLException, java.io.IOException {
Bitmap x;
HttpURLConnection connection = (HttpURLConnection)new URL(url) .openConnection();
connection.setRequestProperty("User-agent","Mozilla/4.0");
connection.connect();
InputStream input = connection.getInputStream();
x = BitmapFactory.decodeStream(input);
return x;
}
I have read a LOT of questions and answers all over the web and I didn't find a good tutorial or solution.
Try to drop this library into your project. Its a useful library.
http://square.github.io/picasso/
Sample Usage:
Picasso.with(context).load("http://example.com/image.jpg").into(imageView);
How to get the drawable from imageView:
Drawable myDrawable = imageView.getDrawable();
You can't convert a Bitmap to a resource id. Resource ids are only for resources in your APK. You'll have to edit or extend the KenBurnsView class to accept Bitmap objects e.g by adding a function like this:
public void setBitmaps(Bitmap... bitmaps) {
for (int i = 0; i < mImageViews.length; i++) {
mImageViews[i].setImageBitmap(bitmaps[i]);
}
}
Then you can pass the Bitmaps that you load with drawable_from_url()
You just forgot to add setDoInput to connection
public 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;
}
}
Add asynchronous task ...
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute(url);
}
public void onClick(View v) {
startActivity(new Intent(this, IndexActivity.class));
finish();
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView mHeaderPicture;
public DownloadImageTask(ImageView mHeaderPicture){
this.mHeaderPicture= mHeaderPicture;
} protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
mHeaderPicture.setImageBitmap(result);
}
}`
Make sure you have the following permissions set in your AndroidManifest.xml to access the internet.
<uses-permission android:name="android.permission.INTERNET" />

how to remove a bitmap from an imageview?

i am decoding an image from a url and i have an array of different urls,i want to show the bitmap images decoded from the url in the imageview but only the image from last url is showing.I am not able to understand the problem.
String [] imageLocation={"http://wallbase1.org/thumbs/rozne/thumb-499842.jpg","http: //ns3002439.ovh.net/thumbs/rozne/thumb-2493796.jpg","http://ns3002439.ovh.net/thumbs/rozne/thumb-2486664.jpg"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image_view = (ImageView)findViewById(R.id.imageview);
while(i<2)
{
bitmap=loadImage(imageLocation[i]);
image_view.setImageBitmap(bitmap);
Animation rotate = AnimationUtils.loadAnimation(LoadImageActivity.this, R.anim.rotate);
findViewById(R.id.imageview).startAnimation(rotate);
i++;
}
}
public Bitmap loadImage(String image_location){
URL imageURL = null;
try {
imageURL = new URL(image_location);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
}
catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}

What is the best way to get an image from URL to ImageView in Android

I am using a method to get the Image from URL when a button is clicked but it gets too much time, about 4-5 seconds to show an image sized only 6 kb using 3G not wi-fi.
My method is this;
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;
}
}
and then;
Bitmap bimage = getBitmapFromURL("http://www.replikler.net/test2.jpg");
ImageView image = (ImageView)findViewById(R.id.movie_image);
image.setImageBitmap(bimage);
do you know another way (faster) to get an image from url ?
imageView.setImageDrawable(Drawable.createFromStream((InputStream)new URL(url).getContent(), "src"));

Show Avatar Facebook to ImageView

I used the code as follows to show up facebook avatar to ImageView
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView) findViewById(R.id.imgAvatar);
img.setImageBitmap(getBitmapFromURL("http://graph.facebook.com/"+"100002394015528"+"/picture"));
}
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;
}
}`
But does it not work. Please help me.
http://graph.facebook.com/id/picture doesn't return an image. It returns some response headers including a 302 redirect, and a location header.
Your example for instance redirects to: http://profile.ak.fbcdn.net/hprofile-ak-snc4/211619_100002394015528_568817_q.jpg
So instead of
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
You need to get the headers from the request, follow the location and then do what you were doing before. I don't know Android, or what language that is. (Java?) So I can't help with that, but I think this might be enough information to get you headed in the right direction.
Use this function for get real URL to avatar:
public static String getUrlFacebookUserAvatar(String name_or_idUser )
{
String address = "http://graph.facebook.com/"+name_or_idUser+"/picture";
URL url;
String newLocation = null;
try {
url = new URL(address);
HttpURLConnection.setFollowRedirects(false); //Do _not_ follow redirects!
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
newLocation = connection.getHeaderField("Location");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newLocation;
}

I am not getting image from url by using this code

I am using the following code
public class Img extends Activity {
/** Called when the activity is first created. */
ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.imgview);
Bitmap bm = getBitmapFromURL("http://l.yimg.com/a/i/us/we/52/21.gif");
if(bm == null)
Toast.makeText(this, "Image can't load", 1).show();
else
img.setImageBitmap(bm);
}
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;
}
}
}
I am getting a message "Image can't load"
Please try below code.
ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.imgview);
Drawable image = getBitmapFromURL("http://l.yimg.com/a/i/us/we/52/21.gif");
if(image == null)
Toast.makeText(this, "Image can't load", 1).show();
else
img.setImageDrawable(image);
}
public Drawable getBitmapFromURL(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
try this..
private Bitmap downloadUrl(String url) {
InputStream myInputStream =null;
Bitmap myBitmap;
StringBuilder sb = new StringBuilder();
//adding some data to send along with the request to the server
sb.append("name=Anthony");
URL url;
try {
url = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStreamWriter wr = new OutputStreamWriter(conn
.getOutputStream());
// this is were we're adding post data to the request
wr.write(sb.toString());
wr.flush();
myInputStream = conn.getInputStream();
wr.close();
myBitmap = BitmapFactory.decodeStream(myInputStream);
} catch (Exception e) {
//handle the exception !
Log.d(TAG,e.getMessage());
}
return myBitmap;
}

Categories

Resources