Importing image from internet into Bitmap - android

I have problem with Bitmap.
I call method:
mStickerListener.onStickerClick(
BitmapFactory.decodeResource(getResources(),
Integer.parseInt(stickerList.get(getLayoutPosition()))));
But the problem is my stickerList.get(getLayoutPosition()))) returns String value(link), because I show images with Picasso, so I get exception:
java.lang.NumberFormatException: For input string: "https://firebasestorage.googleapis.com/v0/b/templatestest-38e3f.appspot.com/o/templates%2Fanimals_templates%2Fdog.png?alt=media&token=3e31962f-df24-493a-91c9-273f3496fa89"
Please, help me solve it!

First of all the exception you are getting says that you are trying to convert a string "https://firebasestorage.googleapis.com/v0/b/templatestest-38e3f.appspot.com/o/templates%2Fanimals_templates%2Fdog.png?alt=media&token=3e31962f-df24-493a-91c9-273f3496fa89" to Integer
The method you are using BitmapFactory.decodeResource takes id of the resource as an input not an url.
You need to use the following code to get bitmap from the url:
try {
URL url = new URL("https://firebasestorage.googleapis.com/v0/b/templatestest-38e3f.appspot.com/o/templates%2Fanimals_templates%2Fdog.png?alt=media&token=3e31962f-df24-493a-91c9-273f3496fa89");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch(IOException e) {
System.out.println(e);
}
Also, add the Internet permission in AndroidManifest.xml as you will be accessing internet for getting stream from an url.
<uses-permission android:name="android.permission.INTERNET" />

Related

Why is BitmapFactory.decodeStream returning null?

Basically I am parsing some JSON that has an image with it and trying to load it into a ImageView. However mBitmap is returning null. I have no idea why and further research has not helped..
Here is an example url I am working with:
http://b.thumbs.redditmedia.com/ivBAJzLMJEkEy9jgTy3z4n-mO7gIGt5mQFU1Al5kJ-I.jpg
Here is all relevant code:
public static Bitmap LoadImageFromUrl(String url){
try {
mBitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
return mBitmap;
}catch (Exception e){
Log.d(TAG,"Error getting image");
return null;
}
}
Here is where the method is called:
mListingModel.setmImageView(LoadImageFromUrl(data.getString(JSON_THUMBNAIL)));
Here is where I set the ImageView:
if(mItem.getmImageView() != null) {
holder.imageView.setImageBitmap(mItem.getmImageView());
}
Note: I am calling the method in an AsyncTask so that is not the problem.
The javadoc for Bitmap.decodeStream() states that:
If the input stream is null, or cannot be used to decode a bitmap, the
function returns null
You're passing it the results of URL.getContent(), which states in its javadoc:
By default this returns an InputStream, or null if the content type of
the response is unknown.
So maybe you should check to see if getContent() returns null before passing it on to the decoder.
i had in the past stupid problems like having my stream reached the end before i decoded it, so ensure to set it's position to 0 before decoding the stream
eg.
stream.Position = 0;
var bmp = BitmapFactory.DecodeStream(stream);
Can you check if this works for you:
InputStream in = new java.net.URL(downloadURL).openStream();
Bitmap avatarBmp = BitmapFactory.decodeStream(in);
in.close();
Also a reminder to add this in your Manifest
<uses-permission android:name="android.permission.INTERNET" />
I had the same problem.
I tried to find decode fail reason.. but I couldn't find.
I just confirmed that InputStream and BufferedInputStream are not null.
After restarting my AVD.. the problem was resolved even though I didn't change any code.

Android & Facebook SDK : decoding pictures from /me/picture graph call

EDIT: Anwser at the end of this post.
I am trying to get a Facebook user's profile picture thanks to the inbuilt Facebook SDK's function Request().
I am using a /me/picture call to get the profile picture and convert it into a Bitmap.
The call is working fine but I don't know how to interpret and parse the result returned from Facebook
Here is the JSON that I get:
{
"FACEBOOK_NON_JSON_RESULT" : "����\u0000\u0010JFIF\u0000\u0001\u0002\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000\u0004*\u0000��\u0000C\u0000\u0006\u0004\u0005\u0006\u0005\u0004\u0006\u0006\u0005\u0006\u0007\u0007\u0006\b"
}
I think this should be representing the profile picture but I don't know what to do with this now.
If somebody could tell me either how to decode it, convert it to a Bitmap or what kind of data it is, I would be grateful.
NOTES: I don't want to use any other function than Request(), like DecodeStream() or an AsyncTask
Here is the answer:
When making a new Request() you need to add the "redirect" parameter to false:
Bundle params = new Bundle();
params.putBoolean("redirect", false);
This will return the correct picture URL:
{
"data":{
"is_silhouette":false,
"url":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-xpa1\/v\/t1.0-1\/p100x100\/10312434_10152395442477210_2933994167675146084_n.jpg?oh=f3c8cb920c97fcfa4dc5e17462445cbf&oe=54404A3A&__gda__=1412730795_5f288e34e5f6e63cb1c3791dcb142880"
}
}
Try this :
ImageView user_picture;
userpicture=(ImageView)findViewById(R.id.userpicture);
URL img_value = null;
img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
userpicture.setImageBitmap(mIcon1);
Where ID is one your profile ID.
For Further details check this :
https://developers.facebook.com/docs/graph-api

android Blob image not loading

here i want to display a image logo which i am receiving from a sqlite as blob database when i dont have internet, though i know its a not a good practice but i want to make it simpler this way.
Problem i am facing is i am able to receive image in log but some warning (link below) is coming because of which it is not loading it into imageView.
Here's the warning i am getting in log !!
I am unable to figure out why it still running DownloadImageTask which is a async task even when not online that is isOnline()==false !!
ImageView logo = (ImageView)findViewById(R.id.Logo);
Bundle extras = getIntent().getExtras();
if(isOnline(this)==true);
{
String link = extras.getString("Logo1");
new DownloadImageTask(logo).execute(link);
}
if(isOnline(this)==false){
Log.d("offline","image");
Log.d("offline",extras.getByteArray("Logo2").toString());
//getting these ^^ in log
byte[] outImage=(byte[]) extras.getByteArray("Logo2");
if(outImage!=null)
{
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
logo.setImageBitmap(theImage);
}
}
}
if(isOnline(this)==true)//you Superfluous add a ";" in this
{
String link = extras.getString("Logo1");
new DownloadImageTask(logo).execute(link);
}
i am a newbie,hope can help you

Android: Get bitmap/drawable from HTTPS

I want to get an image from a server (HTTPS) and show it in an ImageView.
Images are from Facebook Events (example: https://fbcdn-photos-f-a.akamaihd.net/hphotos-ak-prn1/c17.0.50.50/1016204_538791999501573_1791760778_t.jpg )
Drawable.createFromStream((InputStream)new URL("https://fbcdn-photos-f-a.akamaihd.net/hphotos-ak-prn1/c17.0.50.50/1016204_538791999501573_1791760778_t.jpg").getContent(), "src");
throws NullPointer Exception
Or maybe is there any way to get the image via Facebook SDK?
In Facebook SDK I only know the ProfilePictureView (which may only be for profile pictures???)
Thanks so far!
Check <uses-permission android:name="android.permission.INTERNET" />
And I recommend look his:
Android - Loading Image Url and Displaying in ImageView
i recommend you to implement this into your project. It's the best solution.
https://github.com/thest1/LazyList
have You check internet permission?
<uses-permission android:name="android.permission.INTERNET" />
or user LazyLoading. so you can have cache stored in sdcard and next time you can get image quickly. so use example of this
http://www.androidhive.info/2012/07/android-loading-image-from-url-http/
or use another method
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'm such a dumbass :D
Everything of your answers were right!!!
I displayed the ImageView in a dialog.
I used the following to point to the ImageView
ImageView image = (ImageView)findViewById(R.id.imageView_event_picture);
but for sure this is NULL in a dialog!
It has to be
( final Dialog dialog = new Dialog(EventActivity.this); )
ImageView image = (ImageView)dialog.findViewById(R.id.imageView_event_picture);
I didn't mention that I'm running it out of a dialog - Sorry!!

Set ImageView using URL

I have an Image on my server. The URL for the image is something like:
http://www.mydomain.com/123456uploaded_image.jpg
I am trying to set this image to my ImageView. Here is the code that I tried:
try{
String url1 = myeventimagearray[position];
URL ulrn = new URL(url1);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = (InputStream) con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
iveventimg.setImageBitmap(bmp);
else
Log.i("Image","Not set");
} catch(Exception e) {
e.printStackTrace();
}
When I try this, my imageview is empty, i.e., it doesn't set the image view and i get this System err in my logcat:
java.lang.ClassCastException: libcore.net.http.FixedLengthInputStream cannot be cast to com.example.eventnotifier.Base64$InputStream
Base46.java is a file I found from the internet that Encodes and decodes to and from Base64 notation.
Any idea why I'm getting this System.error?
Thank you
Use URlImageViewHelper, it will take care of loading url into imageview.
Refer this
It will take care of caching, loading in background etc. by itself.
This is not an easy answer but you should use a caching system.
See https://github.com/chrisbanes/Android-BitmapCache for an excellent one!
Simply swap the ImageView for NetworkCacheableImageView and then use loadImage( "http://....", true );
You are likely getting an exception because you are trying to do network io on the main thread. Consider using a loader or an AsyncTask to load your image.
Check your logs, I bet you are printing a stack trace in the auto generated catch block.
new Thread(new Runnable() {
public void run() {
final Bitmap b = bitmap = BitmapFactory.decodeStream((InputStream)new URL(myeventimagearray[position]).getContent());
iveventimg.post(new Runnable() {
public void run() {
iveventimg.setImageBitmap(b);
}
});
}
}).start();
Use Picasso to fetch image from url. Implement 'com.squareup.picasso:picasso:2.71828' in build.gradle and in java
Picasso.get()
.load(url) // http://www.example.com/123456uploaded_image.jpg
.resize(50, 50)
.centerCrop()
.into(imageView)

Categories

Resources