display the image url in listview - android

I'm new to android domain..
I'm working with small app..
What i need is ??
I have videos urls and image urls in a array list,which retrive from database as json object and stored in separate array. I want this array list of images should show in listview with text.
How to implement this?? please help me..
I have went through google but still i didn't clear example.. Please any one help me..
Thanks a lot in advance...
public class act extends Activity {
/** Called when the activity is first created. */
static String uri1="http://i3.ytimg.com/vi/bQaWsVQSLdY/default.jpg";
static String uri2="http://i4.ytimg.com/vi/cJQCniWQdno/mqdefault.jpg";
static String uri3="http://i1.ytimg.com/vi/D8dA4pE5hEY/mqdefault.jpg";
public static String[] urls={uri1,uri2,uri3};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView grd=(GridView)findViewById(R.id.gridView1);
grd.setAdapter(new ImageAdapter(this));
grd.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,View v,int pos,long id)
{
Toast.makeText(getBaseContext(),"pic"+(pos+1)+"select ",Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private int itemBackground;
ImageAdapter(Context c)
{
context=c;
TypedArray a=obtainStyledAttributes(R.styleable.Gallery1);
itemBackground=a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
a.recycle();
}
public int getCount()
{
return urls.length;
}
public Object getItem(int pos)
{
return pos;
}
public long getItemId(int pos)
{
return pos;
}
public View getView(int pos,View cv,ViewGroup vg)
{
ImageView imageview=new ImageView(context);
imageview.setImageResource(urls[pos]);
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
imageview.setLayoutParams(new Gallery.LayoutParams(150,120));
imageview.setBackgroundResource(itemBackground);
return imageview;
}
}
}
I try like this..i can't able to get the image...

you can't set it directly as you are trying to do, you will first need to download the image,
store the bitmap, and only then apply the image to you ImageView.
check this question:
How to set image button resource from web url in Android?
this solution is good as well:
How to load an ImageView by URL in Android?

If the requirement is of showing the imageview and textview in list row then try the concept of universal loader.
Link:
https://github.com/nostra13/Android-Universal-Image-Loader

Related

How to load Images with image adapter to gallery from server

I have the following two classes
This is the Image Adapter class:
public class ImageAdapter extends BaseAdapter {
private Context myContext;
private int[] myImageIds = { R.drawable.member, R.drawable.me,
R.drawable.member, R.drawable.me};
public ImageAdapter(Context c) {
this.myContext = c;
}
public int getCount() {
return this.myImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView,
ViewGroup parent) {
ImageView iv = new ImageView(this.myContext);
iv.setImageResource(this.myImageIds[position]);
// Image should be scaled somehow
//iv.setScaleType(ImageView.ScaleType.CENTER);
//iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
//iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
//iv.setScaleType(ImageView.ScaleType.FIT_XY);
//iv.setScaleType(ImageView.ScaleType.FIT_END);
iv.setLayoutParams(new Gallery.LayoutParams(300, 300));
return iv;
}}
And this is the class wich holds the gallery:
public class GreekStore0 extends Activity{
Gallery myGallery;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.store);
myGallery = (Gallery) findViewById(R.id.gallery1);
myGallery.setAdapter(new ImageAdapter(this));
}}
What i want to do is instead of getting the images from the drawablefolder to get them from a server.
For now in the development i want this server to be my localhost. I am using XAMPP to manage a database that is synced with the application in an other part.
I suggest to not reinvent the wheel and instead use a powerful high quality library for this that handles this both smoothly (code and performance wise), such as Square's excellent open source Picasso library.
Example usage:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Picasso is available here:
http://square.github.io/picasso/
Or use the Smoothie library for ListView/GridView specifically:
https://github.com/lucasr/smoothie

Video thumbnail return null

I am creating thumbnails from videos stored in my sd card ,displaying thumbnails and its names in grid view. On item selected event of the grid view pop ups a dialog and asking x, y, right, bottom positions then pasting it to the main activity . I got the video files, and tried to create thumbnail using media store also am retrieving thumbnail as bitmap, but the bitmap is null. In the grid view video names are shown and i am able to select the corresponding thumbnail and can give positions also am able set the thumbnail to the main activity. The problem is the bitmap is null and bitmap image not showing(text vie video name shown). What's the problem ? I can't figure it out? Plz help me? My code is given below. thanks in advance.
if (f.isFile()) {
if (fName.endsWith(".mpg")
|| fName.endsWith(".mov")
|| fName.endsWith(".wmv")
|| fName.endsWith(".rm")
|| fName.endsWith(".mp4")) {
tv.setText(fName);
path = f.getAbsolutePath();
System.out.println("Video file path=>"+path);
thumb = ThumbnailUtils.createVideoThumbnail(f.getAbsolutePath(),MediaStore.Video.Thumbnails.MICRO_KIND);
if(thumb==null)
{
/**Every time it printing null**/
System.out.println("Thumb is null");
}
iv.setImageBitmap(thumb);
From ThumbnailUtils.createVideoThumbnail documentation: May return null if the video is corrupt or the format is not supported.
By default, almost all supported formats are mp4 and 3gp. See here: http://developer.android.com/guide/appendix/media-formats.html for full list of default-supported media formats.
If you are creating thumbnail from sd card video this would create ThumbnailUtils.createVideoThumbnail otherwise use a cursor.
See this example.
Try this code. It is getting the thumbnail of videos from urls. instead of pass the path of sd card .it will help you . Dont forgot to add internet permission in manifest file.
public class VideoThumbnailActivity extends Activity {
public static final String Downloader = null;
static String uri1="http://daily3gp.com/vids/lucky_guy.3gp";
static String uri2="http://daily3gp.com/vids/reporter_hit_by_plane.3gp";
static String uri3="http://daily3gp.com/vids/motorcycle_wipesout_explodes.3gp";
static String uri4="http://commonsware.com/misc/test2.3gp";
public static String uri_array[]={uri1,uri2,uri3,uri4,uri1,uri2,uri3,uri4,uri1,uri2,uri3,uri4};
ImageView imageView;
String url;
Gallery ga1,ga2;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.imageView);
ga1 = (Gallery)findViewById(R.id.gallery1);
ga1.setAdapter(new ImageAdapter(getApplicationContext()));
imageView.setImageBitmap(ThumbnailUtils.createVideoThumbnail(uri_array[0], MediaStore.Video.Thumbnails.FULL_SCREEN_KIND));
//on click event on gallery
ga1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, final int position,long arg3) {
imageView.setImageBitmap(ThumbnailUtils.createVideoThumbnail(uri_array[position], MediaStore.Video.Thumbnails.FULL_SCREEN_KIND));
//on click event on imageview to play video
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),PlayActivity.class);
intent.putExtra("path",uri_array[position]);
startActivity(intent);
}
});
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
#Override
public int getCount() {
return uri_array.length;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View view, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
Bitmap curThumb = null;
curThumb = ThumbnailUtils.createVideoThumbnail(uri_array[position],MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
iv.setImageBitmap(curThumb);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
let me know your problem is resolved or not.

Setting the List<Bitmap> to a GridView in Android

I am developing an app where I have a list of bitmaps and I want to place those bitmaps in a gridView. I have the folleowing code.
My Activity for gridView
public class TestBitmap extends Activity {
private Bitmap bitmap;
private ImageView image;
public static List<Bitmap> splittedBitmaps;
#Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.splitted_grid);
splittedBitmaps = getIntent().getParcelableArrayListExtra("split");
GridView gv = (GridView)findViewById(R.layout.splitted_grid);
gv.setAdapter(new SplittedImageAdapter(this));
}
}
and this is my Adapter class
public class SplittedImageAdapter extends BaseAdapter{
private Context mContext;
public SplittedImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return TestBitmap.splittedBitmaps.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(30,30));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
//imageView.setImageResource(TestBitmap.splittedImages.get(arg0));
imageView.setImageBitmap(TestBitmap.splittedBitmaps.get(arg0));
return imageView;
}
}
I am getting a NullPointerException in the last line of the onCreate method of my Activity class.
Please help me to trace the bug.
In last line of onCreate gb is null. That means (GridView)findViewById(R.layout.splitted_grid); is returning null, which means it is not finding the GridView.
I believe the argument of findViewById is incorrect. R.layout.splitted_grid is a name of layout file. Instead you should use the ID of a view, eg. R.id.your_view_id_here if you have ID specified in your layout XML like that:
<GridView
android:id="#+id/your_view_id_here"
(...)
/>

screen rotation and live update gridview android

i have a big doubt about memory leak and screen rotation. I know that activity is destroy when that happens and new one is started. With my code, with is the best way to show the gridview? (this activity is part of a tab activity so in the main one i get the pictures from web services and put them on the object, using AsyncTask)
i would like to show the pictures at the same time they are saved on the object. Like simulating ajax...is that possible? at the moment, they are all show on the start or resume.
Also, im facing memory leak here?
last question, how can i show photos in the grid
my code:
public class LoteFotosActivity extends Activity {
Sistema sis=Sistema.getInstance();
Lote lote;
GridView gridview;
ImageAdapter iA;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lote_foto_view);
Bundle extras = getIntent().getExtras();
lote=sis.getLoteDetalle(extras.getInt("LoteID"));
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(iA=new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(LoteFotosActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return lote.FOTOS.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
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(200,200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(lote.FOTOS.get(position));
return imageView;
}
}
//test looking
public void onResume() {
super.onResume();
Toast.makeText(this,"ON RESUME",
Toast.LENGTH_SHORT).show();
// gridview.setAdapter(new ImageAdapter(this));
// foto.setImageDrawable(lote.FOTOS.get(0));
}
public void onPause() {
super.onPause();
Toast.makeText(this,"onPause",
Toast.LENGTH_SHORT).show();
iA=null;
gridview.setAdapter(iA=new ImageAdapter(this));
// foto.setImageDrawable(lote.FOTOS.get(0));
}
thx in advance !
I think you should add this to your activity
android:configChanges="orientation"
to avoid the destroying problem when rotating the screen... with this you dont need to save state to restore, there is no need because the activity is not destroyed and recreated everytime the screen rotation changes
For API 13+ the screenSize must also be handled:
android:configChanges="orientation|screenSize"

Getting an image to link to a web page in a GridView on Android?

I have an app with the GridView layout have a single image for testing purposes until I figure out this next step. I have everything set up, but I don't know how implement a design where image A, B, C, D, etc. are clicked result in the user (me) landing on a webpage that I specify. I need each image to link to different locations and I would really appreciate the help to implement this into my code:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create new ImageView for each item
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to images
private Integer[] mThumbIds = {
R.drawable.A, R.drawable.B,
R.drawable.C, R.drawable.D,
R.drawable.E, R.drawable.F,
R.drawable.G, R.drawable.H,
};
}
Use the getItemId() in your adapter (just return the position) to select the URL you want to go to. Just like you have mThumbIds, just have a parallel array that holds the matching URLs, so when an image is clicked, its corresponding URL can be accessed easily at the same index.
The "zero-work" method of doing this would be to simply launch an Intent that would open the URL in the device's browser from an OnItemClickListener. Something like:
//Obtain a reference to the view in your layout somehow...I chose findViewById()
GridView grid = (GridView)findViewById(R.id.peters_grid);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//"position" is the location of the image clicked
String url = arrayOfUrls[position];
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
Where you are grabbing the correct URL for each position from an array of Strings. This is just one idea for getting the right URL...you could construct the proper URL a million different ways, but the key is to pass it to the Intent and then fire it.
The "little work" version of this would be to create a WebView in this or another Activity so you can load the web page and stay within your application code. If the WebView is in a second Activity, it would still be best to use an Intent to pass the URL string from the first Activity to the second.
Hope that Helps!

Categories

Resources