Android: Get bitmap/drawable from HTTPS - android

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!!

Related

Importing image from internet into Bitmap

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" />

loading full image from url into image view with any image loading library

I'm downloading an image from a URL and displaying it in an ImageView. I need to download the image at its full original size. I've tried Glide, Picasso and Universal Image Loader with no success. Is there any library or mehod out there to achieve this? I even tried making my own AsyncTask to do it, something like this:
public class ImageLoader extends AsyncTask<Void, Void, Bitmap> {
#Override
protected Bitmap doInBackground(Void... params) {
try {
URL url = new URL(bundle.getString("selectedImage"));
HttpURLConnection conn =
(HttpURLConnection)url.openConnection();
conn.setReadTimeout(6000);
conn.setConnectTimeout(6000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int respose = conn.getResponseCode();
InputStream is = conn.getInputStream();
BufferedInputStream bufferedInputStream = new
BufferedInputStream(is);
bitmap = BitmapFactory.decodeStream(bufferedInputStream);
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
but no success. Anyone have anything to help me?
1) Try to use Volley library.
https://developer.android.com/training/volley/request.html#request-image
2) Use WebView instead ImageView
I'm not really sure what you mean by "its full original size". I haven't experienced any automagic scaling of images simply by downloading them.
Maybe you could double-check that you have an appropriate android:scaleType on the target ImageView. You can read more on the different values of the scale type property here: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
If you want to pan the image, like an "unscaled" web page in the browser (typically when the image is bigger than the screen), you might need to add further logic to manage this. Maybe it could be as easy as having a ScrollView wrap your ImageView (which then would wrap its content, of course).
The error was down to photo bucket giving me a scaled down URL instead I used flikr and on my device I get an image almost identical to my original (Picasso limit is 2048x2048) but on other devices I still seem to get a 1080 x 910 image, will investigate further but it seems the answer is not to use photo bucket

How can i "update" a banner in Android?

I have an app where I have one banner in the top with News, when I want to put other news I need to open the code and change the resource .jpg and the Link. There is a way to change the banner and the Link (or at least the banner) without modifing the code? Idk maybe uploading it to a webpage or something like this.
thanks
My suggestion would be to upload a banner.jpg to a server that your app can access and dynamically load. This would prevent having to update your app every time you want to change the banner, and makes it cleaner (no excessive Google Play updates). To do actually load the image you can use this code:
ImageView image1 = (ImageView) findViewById(R.id.mybanner);
new Thread(new Runnable(){//create a new thread so we can do network operations
#Override
public void run() {//main thread function
try {//attempt to do network stuff
URL url = new URL("http://your-hosting-site.com/banner.jpg");//create aURL object with the path to your banner
HttpURLConnection con = (HttpURLConnection) url.openConnection();//create the connection object from the url
con.setReadTimeout(15000);
con.setConnectTimeout(15000);
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect();//connect to the server
InputStream is = con.getInputStream();//get the stream so we can read the image
Drawable d = Drawable.createFromStream(is, "MyBanner");//create a drawable from the image
Bitmap bmp = ((BitmapDrawable) d).getBitmap();//create a bitmap from the drawable
final Drawable dS = new BitmapDrawable(Bitmap.createScaledBitmap(bmp, 192, 192, true));//scale it to whatever size you need
con.disconnect();//disconnect now that we're done
runOnUiThread(new Runnable(){//run UI update code on the main thread
#Override
public void run() {
image1.setImageDrawable(dS);//set the imageview to the banner we downloaded
}
});
} catch (MalformedURLException e) {//catch url error
e.printStackTrace();
} catch (IOException e) {//catch io error when downloading
e.printStackTrace();
}
}
}).start();//run the thread
Change "http://your-hosting-site.com/banner.jpg" (line 6) to wherever you uploaded the .jpg, R.id.mybanner (line 1) to the id of your ImageView, and "MyBanner" (line 14) to whatever you want to call the image.
You might want to save your banner to the phone and only check after X days/hours for an update to save data, but that is up to you.

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)

Size image to fit WebView dimensions

An API I hit returns a URL to an image. I want to use a WebView, feed it this URL and have it display the image. However, I want the WebView to be a static 90dip x 90dip (images are square). The images are bigger than 90px. Is there a way I can tell the WebView to size the image to its own dimensions?
Have you tried it yet? Does it not work, if not what does it do instead?
I think you could use an ImageView to display the image with no problems. You can use a method like this to return to you a Drawable object from a url, which you can then set to the ImageView with setImageDrawable(img);
/***********************************************************
* This method will return the image from a URL.
* Note: This should be called from the UI thread.
***********************************************************/
public Drawable getRemoteImage(final URL aURL) {
try {
final URLConnection conn = aURL.openConnection();
conn.connect();
final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
final Drawable d = Drawable.createFromStream(bis, "src");
bis.close();
return d;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
ImageView's I know for a fact you can set to a static size (90dip x 90dip) and it will handle scaling the image down if need be to make it fit in that size, WebView might try to make it scrollable or something, I am not sure.

Categories

Resources