Internet url as Android ImageSpan source - android

is it possible to specify internet url for ImageSpan and get it shown with TextView? I've tried quite a few versions of
String mockContent = "<img src=\"http://www.google.com/intl/en_com/images/srpr/logo3w.png\">";
myTextView.setText(Html.fromHtml(mockContent), BufferType.SPANNABLE);
but that results in the generic not found image (blueish black bordered square).
Do I have to download the image first, then replace all the urls and so on?
Knowing the new C# is capable of handling such resources, I hoped Android might meet my hopes here.
Using API version 10: 2.3.3.

If you have the image source locally then it should work or else use,
Html.fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
Returns displayable styled text from the provided HTML string.
Nice example I found on web,
String s = Html.fromHtml(htmlContent, new ImageGetter() {
#Override
public Drawable getDrawable(String source) {
// Code for getting image either by download or using static iamge
Drawable d = null;
try {
InputStream src = imageFetch(source);
d = Drawable.createFromStream(src, "src");
if(d != null){
d.setBounds(0,0,(d.getIntrinsicWidth(),
d.getIntrinsicHeight());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return d;
}
},null);
For more info Html.fromHtml with ImageGetter.

Related

I am facing java.net.MalformedURLException: no protocol: While converting url to bitmap in android

I am trying to convert url to bitmap and then set that bitmap to background as wallpaper. And all this process is getting done in background with the use of worker class in android. But I am getting no protocol error. I am fetching any one random wallpaper link from firebase then I am trying to convert it into bitmap by
try {
URL url = new URL(image_url);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);
} catch(IOException e) {
Log.e("tag102",e.getMessage());
}
But this method gave me this error in logcat
2022-07-08 03:15:07.544 25513-25752/com.nightowl.stylo E/tag102: no protocol:
But when I put hardcoded string (showed in below method) in url method parameter then it return bitmap without any error. and wallpaper also gets changed
try {
URL url = new URL("https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);
} catch(IOException e) {
Log.e("tag102",e.getMessage());
}
But i want random string to set and get me bitmap from that. So how i can achieve that? I also try to encode url by
try {
encodedURL = URLEncoder.encode(image_url, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e("tag3",e.toString());
}
I am using three kind of url in this project
https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800
https://cdn.pixabay.com/photo/2020/11/27/22/07/naruto-5783102_960_720.png
https://images.unsplash.com/photo-1641414315243-196e7382c32d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1178&q=80
Any help will be appreciated.

Using image from street view

I want to get a picture of streetview (stating latitude and longitude) and display in a dialog, is this possible?
I saw some examples here, but not found one that show me how to display the image in dialog.
Sorry if already exists this question in the site, but I not found when I search.
Yes you can,
as a URL root you can use this one http://cbk0.google.com/ or maps.google.com
and this is a example where you use above mentioned URL by providing location:
http://cbk0.google.com/cbk?output=xml&hl=x-local&ll=34.058593,-118.240673&it=all
The result should be:
<panorama>
<data_properties image_width="13312" image_height="6656" tile_width="512" tile_height="512" pano_id="70o9Pukc2KSjO-PfeHussw" scene="1" imagery_type="2" level_id="ffffffffb3840000" num_zoom_levels="5" lat="34.058620" lng="-118.240693" original_lat="34.058620" original_lng="-118.240693" best_view_direction_deg="109.819">
<copyright>© 2013 Google</copyright>
<text/>
<street_range/>
<region/>
<country/>
</data_properties>
<projection_properties projection_type="spherical" pano_yaw_deg="93.25" tilt_yaw_deg="-180" tilt_pitch_deg="0"/>
<annotation_properties>
<link yaw_deg="252.58" pano_id="qciD6ogWmkxiq4p3OaprjA" road_argb="0x80fdf872" scene="1">
<link_text/>
</link>
<link yaw_deg="38.52" pano_id="lqiWuIrIXa_86In3RRxB1w" road_argb="0x80fdf872" scene="1">
<link_text/>
</link>
</annotation_properties>
<levels>
<level level_id="ffffffffb3840000" pano_id="70o9Pukc2KSjO-PfeHussw" ordinal="0">
<text/>
<abbreviation/>
</level>
</levels>
</panorama>
From XML you fetch pano_id, (in my case pano_id="70o9Pukc2KSjO-PfeHussw")
and after you can extract image based on pano_id:
http://cbk0.google.com/cbk?output=tile&panoid=70o9Pukc2KSjO-PfeHussw&zoom=3&x=5&y=1
Other way to get XML from maps.google.com:
http://maps.google.com/cbk?output=xml&ll=32.051626,34.7613
to load Image from URL, there is a lot options:
Drawable
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;
}
}
or to Bitmap:
try {
ImageView i = (ImageView)findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Hope it will help you,

Set a Parsed (JSON) image into background of a layout? [duplicate]

This question already has answers here:
Android - How to download an image and use it as new resource?
(4 answers)
Closed 9 years ago.
I have got a demo image link :
http://madhabpurps.org/wp-content/uploads/2013/04/28-239x300.jpg
I want to set the image in the background of a layout inside the view holder class:
static class ViewHolder {
TextView txtName;
TextView txtCityState;
RelativeLayout rl;
}
holder.txtName.setText(searchArrayList.get(position).getTitle());
holder.txtCityState.setText(searchArrayList.get(position).getDescription());
I have to set the image from the link here, I have tried this line of code but it's showing error.
holder.rl.setBackgroundResource(searchArrayList.get(position).getImage());
I get answer from here change background image of Framelayout via URL
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, saveFilename);
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
and then you can use it like this
Drawable drw = ImageOperations(this,url,filename)
rl.setBackgroundDrawable(drw);
This should fix. But for general case I recommend another way to solve these problems.
I recommend using a well documented image downloading and caching library. I am using picasso http://square.github.io/picasso/ . Setup it, using library is easy.
Then you can fill an imageview by just writing
Picasso.with(activity)
.load(url)
.fit()
.into(imageView);
use the below code
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
Drawable d = new BitmapDrawable(getResources(),bitmap);
rl.setBackgroundDrawable(dr);

Get Meta Data of Image - Android

http://www.isco.com/webproductimages/appBnr/bnr1.jpg
I've used a website to see the metadata of a image. In this website, it shows all info of image. I want to know how to get the "Title" tag of above image in android.
I found here the similiar question for iOS only: How to get image metadata in ios
However I don't know how to get "Meta data" of image on android. ExifInterface only gives some informations. But I'm unable to get "Title" tag with it.
Can you provide any code snippet for get meta data in android for image?
Download metadata extractor from the link given here ...... click to download the library
choose the version 2.5.0.RC-3.zip
Extract the jar Folder
and import jar into libs folder in your poject and then execute the below code
try {
InputStream is = new URL("your image url").openStream();
BufferedInputStream bis = new BufferedInputStream(is);
Metadata metadata = ImageMetadataReader.readMetadata(bis,true);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
System.out.println(tag);
}
}
}
catch (ImageProcessingException e){}
catch (IOException e) {}
If you want to retrieve meta data information about an image ExifInterface is what you are looking for. Here is a quite good example of how this interface is used: http://android-er.blogspot.com/2009/12/read-exif-information-in-jpeg-file.html
But if you want to retrieve information of an online image I'm afraid it's not yet possible.
If you want to retrieve metadata from image in Android project, then you can do that with the help of: https://github.com/drewnoakes/metadata-extractor
Implement this in your gradle using
implementation 'com.drewnoakes:metadata-extractor:2.12.0'
Complete code is as follows
private static String getImageMetaData(Uri image1) {
try {
InputStream inputStream = FILE_CONTEXT.getContentResolver().openInputStream(image1);
try {
image1.getEncodedUserInfo();
Metadata metadata = ImageMetadataReader.readMetadata(inputStream);
for (Directory directory1 : metadata.getDirectories()) {
if (directory1.getName().equals("Exif IFD0")) {
for (Tag tag : directory1.getTags()) {
if (tag.getTagName().equals("Date/Time")) {
return tag.getDescription();
}
}
}
}
} catch (ImageProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}

ListView scrolling lag in android

I am having a customized list view in my application, which is showing an image and text.
The Image I am getting from URL, using the code below:
private static Drawable ImageOperations(Context ctx, String url,
String saveFilename) {
try {
InputStream is = (InputStream) fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
all is working perfect, except the list view scrolling, its very slow. If I disable the images, the scroll speed smooth-ens , but with the image enabled, it lags alot.
Is there any possible way I can reduce or remove this lagging?
Thanks
You need to do your fetching in the background.
One of the examples you can use :
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
use this library for downloading images in background and caching..
it won't hurt the UI
https://github.com/koush/UrlImageViewHelper
Are you lazy loading your images? See this question for details.

Categories

Resources