I am new to Android, I am developing an app, where in I search the youtube using the
....
YouTubeService service = new YouTubeService('blah','blah')
....
VideoFeed videoFeed = service.query(query, VideoFeed.class);
List<VideoEntry> videos = videoFeed.getEntries();
YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
String webPlayerUrl = mediaGroup.getPlayer().getUrl();
......
ytv.setWebPlayerUrl(webPlayerUrl);
.....
List<String> thumbnails = new LinkedList<String>();
for (MediaThumbnail mediaThumbnail : mediaGroup.getThumbnails()) {
thumbnails.add(mediaThumbnail.getUrl());
}
....
In the Adapter, I used this
mWebView.loadUrl(torvideo.getWebPlayerUrl());
But I get a full screen youtube image in the webview and a scroll able list of similar videos.
However What I want is a thumbnail which looks similar to youtube search result (Small Image, title of the video and few more details.
Can you please suggest me, which view or a sample code, so that I can use the thumbnails list I got it from mediagroup object to display in my app?
Edit/Update:
I have used
public class ResultViewAdapter extends BaseAdapter {
........
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
final LayoutInflater li = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = li.inflate(R.layout.activity_result_list, null);
}
TORYouTubeVideo torvideo = (TORYouTubeVideo) getItem(position);
TextView textView = (TextView) row.findViewById(R.id.textView1);
textView.setText(torvideo.getVideoTitle());
InputStream is = (InputStream) new URL(torvideo.getThumbnails().get(0)).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
ImageView imageView = (ImageView) row.findViewById(R.id.imageView1);
imageView.setImageDrawable(d);
return row;
}
But still I cannot see the images in my list view and Only see an empty scrollable list
Thanks
Answering my question again!!
Moved this part
InputStream is = (InputStream) new URL(torvideo.getThumbnails().get(0)).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
To the same async task where my youtube query is happening and I am able to see the image now!
(It was the issue with mainThread)
Looks like long way to go with Android!! :P
Thanks
Related
I am using a Class that implements GoogleMap.InfoWindowAdapter.
I want to show an image loaded from internet inside the info window:
#Override
public View getInfoContents(final Marker m) {
//Carga layout personalizado.
View v = inflater.inflate(R.layout.info_windows_origen, null);
String info = m.getTitle();
String direccion = m.getSnippet();
String url = m.getSnippet();
((TextView)v.findViewById(R.id.info_window_nombre)).setText("PUNTO DE PARTIDA");
((TextView)v.findViewById(R.id.info_window_placas)).setText(info);
((TextView)v.findViewById(R.id.info_window_estado)).setText(direccion);
ImageView imgProfile =(ImageView)v.findViewById(R.id.info_window_imagen);
SharedPreferences prefs =
getApplicationContext().getSharedPreferences(MISDATOS, Context.MODE_PRIVATE);
String imagen = prefs.getString("imagen", "por_defecto#email.com");
Log.d("BOTON ORIGEN ","mi imagen "+urlProfileImg+imagen);
Picasso.with(getApplicationContext()).load(urlProfileImg+imagen).fit().into(imgProfile);
return v;
}
I have checked both, the image URL and the image name, and both are correct, but the image is not shown.
Be sure you have internet permission in the manifest.
Try removing fit
Posting the url you are providing would be easier to work with
How to display image from sql database to listview in android xamarin using webservices and json?
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = objList[position];
if (convertView == null)
{
convertView = objActivity.LayoutInflater.Inflate(Resource.Layout.ContListViewHospName, null);
}
convertView.FindViewById<TextView>(Resource.Id.tvHospID).Text = item.HospID;
convertView.FindViewById<TextView>(Resource.Id.tvHospName).Text = item.HospName;
byte[] img = (byte[])item.HospLogo;
Bitmap bitmap = BitmapFactory.DecodeByteArray(item.HospLogo, 0, item.HospLogo.Length);
convertView.FindViewById<ImageView>(Resource.Id.imgLogo).SetImageBitmap(bitmap);
return convertView;
}
i store binary value in one string variable then i use json to transfer values to my cs file in my android project
Your store binary value in one string should looks like this: "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAAAIABJREFUeJzsvfd33EiW7/mJgEtHb0WKFGWqSuW96epqM9P9+s30mZ/2j33n7O7Zt/Nme8zr6emu6i5fk........"
That is base64 bytes. You can not just use (byte[]) to convert the string to byte[]:
you need to use System.Convert.FromBase64String() function.
//The string is download form server in the json file
string myBytes = "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAAAIABJREFUeJzsvfd33EiW7/mJgEtHb0WKFGWqS........"
byte[] decByte3 = System.Convert.FromBase64String(myBytes);
And then convert the bytes to image:
public Bitmap Bytes2Bimap(byte[] b)
{
if (b.Length != 0)
{
return BitmapFactory.DecodeByteArray(b, 0, b.Length);
}
else
{
return null;
}
}
and convert the bitmap to drawable :
Bitmap myIcon = Bytes2Bimap(decByte3);
Drawable myIconD = new BitmapDrawable(myIcon);
imageView1.Background = myIconD;
May be your image is too big try to scale it, like here
Also make sure you have set proper dimension in your layout like width and height.
What can be cause for this error:
java.lang.OutOfMemoryError: Failed to allocate a 10111384 byte allocation with 1305618 free bytes and 1275KB until OOM
Error happens in this line :
Bitmap bmp = BitmapFactory.decodeByteArray(currentTour.getTourImage(),0,currentTour.getTourImage().length);
My ListView adapter View :
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getLayoutInflater().inflate(R.layout.tour_item, parent, false);
if (position % 2 == 1) {
view.setBackgroundColor(Color.rgb(189, 230, 247));
} else {
view.setBackgroundColor(Color.WHITE);
}
TourAnounceRow currentTour = tourAnounceList.get(position);
if (currentTour != null) {
TextView tourName = (TextView) view.findViewById(R.id.lblTourName);
tourName.setText(currentTour.getTourName());
TextView place = (TextView) view.findViewById(R.id.lblPlace);
place.setText(currentTour.getPlace());
RatingBar tourStarCount = (RatingBar) view.findViewById(R.id.ratingBarTourStarCount);
tourStarCount.setRating(Float.parseFloat(String.valueOf(currentTour.getHotelStarsCount())));
TextView adultCount = (TextView) view.findViewById(R.id.lblAdultCount);
adultCount.setText(String.valueOf(currentTour.getAdultCount()));
TextView childrenCount = (TextView) view.findViewById(R.id.lblChildCount);
childrenCount.setText(String.valueOf(currentTour.getChildrenCount()));
TextView mealCount = (TextView) view.findViewById(R.id.lblMealCount);
mealCount.setText(String.valueOf(currentTour.getDailyMealCount()));
TextView price = (TextView) view.findViewById(R.id.lblPrice);
price.setText(String.valueOf(currentTour.getPrice()));
ImageView ivTourImage = (ImageView) view.findViewById(R.id.ivTourImage);
ivTourImage.setScaleType(ImageView.ScaleType.FIT_XY);
Bitmap bmp = BitmapFactory.decodeByteArray(currentTour.getTourImage(), 0,currentTour.getTourImage().length);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.WEBP, 10, stream);
ivTourImage.setImageBitmap(bmp);
}
return view;
}
This happens because your bitmap it's to large, you can optimize the load of the bitmap, take a look to this link
I would suggest, especially since you're using this inside a ListView, that you take a look at the Glide Library (https://github.com/bumptech/glide). The library will handle both an asynchronous call to load the image, as well as the resizing of the image to fit your ImageView (saving memory).
You also seem to be hold a lot of byte[] images in memory (based on tourAnounceList), you want to only hold a reference to them when possible; either as a URL/URI or a local resource integer (R.drawable.my_image).
The code should look like this:
ImageView ivTourImage = (ImageView) view.findViewById(R.id.ivTourImage);
Glide.with(parent.getContext()).load(currentTour.getImageUrl())
.fitCenter().into(ivTourImage);
I have 20 URL of different images. I need to download the 20 images from the URL and display it in Grid view. For me its taking much time to download the image content. Following is the code i'm using in Image Adapter class.
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(220, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1,0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageDrawable(GetDrawableImage(url));
return imageView;
}
private Drawable GetDrawableImage(String zurlP)
{ InputStream InputStreamL = null;
Drawable DrawableImageL = null;
try {
InputStreamL = (InputStream) new URL(zurlP).getContent();
DrawableImageL = Drawable.createFromStream(InputStreamL, "src");
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return DrawableImageL;
}
Is there any easiest way(Less Time Consuming) to perform the same task?
Yes.., You can use Asynctask or Painless threading to load large images from server. And Cache the Images once you download. You can use [Fedor lazylist] for that (https://github.com/thest1/LazyList)
Various possibilities.
Multi Threading
Cacheing
Pain less threading
Large bitmaps loading
Or you can simply go with thumb nails. Means get thumb nails from server instead of bit images. Loading will be effective
You need to download images in other thread. You can use image loader.
Help please!
In my application I use Android-UiTableView ( thiagolocatelli / android-uitableview ) and AQuery ( androidquery / androidquery )
My task is to make the loading of data from json array and load image from URL.
Load a list without pictures I was able to:
jgall = json.getJSONArray("gall");
int z = 0;
for(int i = 0; i < jgall.length(); i++){
JSONObject c = jgall.getJSONObject(i);
tableView.addBasicItem(c.getString("name"), c.getString("place"));
}
I try to show a customized view and nothing happens
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout view = (RelativeLayout) mInflater.inflate(R.layout.custom_view,null);
String iurl = c.getString("img");
aq.id(R.id.img).image(iurl, false, true);
ViewItem viewItem = new ViewItem(view);
tableView.addViewItem(viewItem);
Tell me it's real to make using Android-UiTableView? if so! how?
I have done this by modifying the library code myself.
I have added the following constructor at UITableView.java:
public void addBasicItem(Drawable drawable, String title, String summary) {
mItemList.add(new BasicItem(drawable, title, summary));
}
And the following at BasicItem.java:
public Drawable getDDrawable() {
return dDrawable;
}
public BasicItem(Drawable ddrawable, String _title, String _subtitle) {
this.dDrawable = ddrawable;
this.mTitle = _title;
this.mSubtitle = _subtitle;
}
And once again modified the setupBasicItem method at UITableView.java file:
((ImageView) view.findViewById(R.id.image)).setBackgroundDrawable(item.getDDrawable());
Finally, at your code add the following:
URL newurl = new URL("http://www.example.com/example.png");
Bitmap bmp = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
Drawable drawable = new android.graphics.drawable.BitmapDrawable(getResources(),bmp);
tableView.addBasicItem(drawable, "Title", "SubTitle");
Hope it helps, cause it works for me.