I'm looking to add images from my SD card storage into an Interger array list.
At the moment I can display images from my drawable folder because they are (somehow) in int format, for example: Log.d("MyTag", R.drawable.ic_launcher_background)); currently returns me 2131230825.
I can access my SD card images in a way to return me: /storage/emulated/0/Android/data/com.hangr.hangr/files/Pictures/Hangr_20181119__153130.jpg (String format). And I can also make bitmaps out of them with "Bitmap myBitmap = BitmapFactory.decodeFile(f.get(i));" (Bitmap format).
Any idea on how I can pass these string/bitmap forms into an int object?
package com.myapp.myapp;
imports ...
public class test extends Activity {
public ArrayList<Integer> mThumbIds = new ArrayList<>();
File[] listFile;
ArrayList<String> f = new ArrayList<String>();// list of file paths
ArrayList<Bitmap> myBitmapArrayList = new ArrayList<Bitmap>();// list of bitmaps
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
getFromSdcard();
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
// Test: adding drawable items to my interger list
mThumbIds.add(R.drawable.logo);
}
//************** important function that calls from my SD card
public void getFromSdcard(){
File file = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if (file.isDirectory()) {
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++)
{
f.add(listFile[i].getAbsolutePath());
Bitmap myBitmap = BitmapFactory.decodeFile(f.get(i));
fml.add(myBitmap);
}
}
//************** TESTING THE VALUE OF MY VARIABLES
Log.d("MyTag", "File file " + file); ///storage/emulated/0/Android/data/com.myapp.myapp/files/Pictures
Log.d("MyTag", "File length " + file.isDirectory()); //true
Log.d("MyTag", "First element in f: " + f.get(0)); ///storage/emulated/0/Android/data/com.myapp.myapp/files/Pictures/20181119__153130.jpg
Log.d("MyTag", "All the Bitmaps? " + myBitmapArrayList); //returns huge list of bitmaps, i.e. android.graphics.Bitmap#39bd71a
Log.d("MyTag", "Drawables? " + (R.drawable.ic_launcher_background)); //returns me 2131230825
}
public class ImageAdapter extends BaseAdapter {
//************** code to zoom in on images, from https://www.androidbegin.com/tutorial/android-gridview-zoom-images-animation-tutorial/
public View getView(final int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds.get(position));
// imageView.setImageListener(bottoms_Listener);
imageView.setTag(mThumbIds.get(position));
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int id = (Integer) arg0.getTag();
zoomImageFromThumb(arg0, id);
}
});
return imageView;
}
private void zoomImageFromThumb...
}
Before you get mad at me for not "trying", I have. The hardest part was pulling from the device storage and accessing the photos in there. I just dont get why drawable images are ints and why there's no seemingly straightforward way I can turn my images into ints for similar use.
You can't get their integer references, because they don't have any.
R.whatever (R.drawable, R.string, R.xml, etc) are classes generated by Android Studio that hold integer fields whose names correspond to your resources. When you use something like getDrawable(), Android uses the integer you passed to find the corresponding resource and load it as an image. This is all done because the resources are compiled and stored in the APK itself, and aren't accessible with paths.
However, images on internal storage or your SD card aren't resources. They have directly accessible paths, and aren't inside any APKs. Android doesn't give them integers IDs because they don't need them, and it's just not how it works.
To get images from storage, you need to use paths.
Related
Update #2
Thanks to AgentP for hinting my problem . I have fixed this issue by making this change inside showImages() :
(Also created a global reference field String path; in the same activity) :
private void showImages(){
// Added following two lines :
ContextWrapper cw = new ContextWrapper(this); // + added
path = cw.getDir("files", Context.MODE_PRIVATE).toString(); // + added
// String path = DrawingActivity.path; // - removed
allFilesPaths = new ArrayList<>();
allFilesPaths = listAllFiles(path);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.galleryRecycleViewId);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 3);
recyclerView.setLayoutManager(layoutManager);
ArrayList<Cell> cells = prepareData();
ImageAdapter adapter = new ImageAdapter(getApplicationContext(), cells);
recyclerView.setAdapter(adapter);
}
Update
Apparently I have a static string that is only set inside save image method . It's purpose is to give image folder path to the show images method. I'm trying to provide path to my showImages() method via other means now .
Old
I have a gallery activity that displays images from a folder with recycler view .
When I start my app the gallery is empty despite having images in the folder .
When I go to my drawing activity , save an image and return to gallery activity it shows all images without problems .
I placed a log inside onbindviewholder method and it only executes after I save an image.
What functionality is missing for image adapter to see exiting files and what does image saving do to make it find them when executed ?
(Also I remember when I wrote the save image method that it wanted me to do #SuppressLint("WrongThread") , but now it works without it)
Debug info when opening gallery without saving a file in drawing activity:
ClassLoader referenced unknown path: /data/app/com.example.myapp-2/lib/x86_64
Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
I/OpenGLRenderer: Initialized EGL, version 1.4
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
Methods involved in displaying images inside Gallery Activity :
List <Cell> allFilesPaths;
// on create calls show images after checking if read storage permission is good
private void showImages(){
// this is the current problem line ,
// I need a way to give path of my image folder to this method
// Check SaveImage() method below to see how it is initially set
// path is a global static field inside drawing activity
String path = DrawingActivity.path;
allFilesPaths = new ArrayList<>();
allFilesPaths = listAllFiles(path);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.galleryRecycleViewId);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 3);
recyclerView.setLayoutManager(layoutManager);
ArrayList<Cell> cells = prepareData();
ImageAdapter adapter = new ImageAdapter(getApplicationContext(), cells);
recyclerView.setAdapter(adapter);
}
private ArrayList<Cell> prepareData(){
ArrayList<Cell> allImages = new ArrayList<>();
for (Cell c : allFilesPaths){
Cell cell = new Cell();
cell.setTitle(c.getTitle());
cell.setPath(c.getPath());
allImages.add(cell);
}
return allImages;
}
private List<Cell> listAllFiles(String pathName){
List<Cell> allFiles = new ArrayList<>();
if(pathName != null){
File file = new File(pathName);
File[] files = file.listFiles();
if(files != null){
for (File f : files){
Cell cell = new Cell();
cell.setTitle(f.getName());
cell.setPath(f.getAbsolutePath());
allFiles.add(cell);
}
}
}
return allFiles;
}
Inside my Image Adapter :
private ArrayList<Cell> galleryList;
private Context context;
private static final String TAG = "ImageAdapter";
public ImageAdapter(Context context, ArrayList<Cell> galleryList) {
this.context = context;
this.galleryList = galleryList;
}
#NonNull
#Override
public ImageAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell, parent, false);
return new ImageAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ImageAdapter.ViewHolder holder, final int position) {
holder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
Log.d(TAG, "onBindViewHolder: " + galleryList.size());
setImageFromPath(galleryList.get(position).getPath(), holder.img);
holder.img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String path = galleryList.get(position).getPath();
Intent intent = new Intent(context ,ImagePreview.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("imagePath",path);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return galleryList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView img;
public ViewHolder(#NonNull View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.img);
}
}
private void setImageFromPath(String path, ImageView image){
File imgFile = new File(path);
try {
Bitmap myBitmap = BitmapFactory.decodeStream(new FileInputStream(imgFile));
image.setImageBitmap(myBitmap);
// ImageView imageView = (ImageView)findViewById(R.id.imageViewSelect);
// imageView.setImageBitmap(bitmap);
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
method inside drawing activity that saves images :
public void saveImage() {
ContextWrapper cw = new ContextWrapper(getContext());
String filename = "img" + System.currentTimeMillis();
File directory = cw.getDir("files", Context.MODE_PRIVATE);
path = cw.getDir("files", Context.MODE_PRIVATE).toString();
File myPath = new File(directory, filename + ".jpg");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(myPath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
}catch(Exception e){
e.printStackTrace();
}finally {
try {
fileOutputStream.flush();
fileOutputStream.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
Make sure the variable DrawingActivity.path in showImages() have some value inside of it. It might not have initialized properly.
Try putting this in the ViewHolder Class constructor instead
setImageFromPath(galleryList.get(getLayoutPosition()).getPath(), holder.img);
I've got the following code from "Android in Practice" book. It's implementation of custom adapter which downloads images from the internet. It uses private class RetrieveImageTask to retrieve pictures.
Can someone explain me why the first thing the adapter class does is to get image from the cache instead of downloading it ? I understand it in a way that, the first time it displays
default image which was set in the beginning of getView(), then sets downloaded image, but does it mean that view is being refreshed constantly by calling getView() ?
And why author sets tag of image to item ID in getView() and then sets it to null in onPostExecute() ?
DealsAdapter
private class DealsAdapter extends ArrayAdapter<Item> {
public DealsAdapter(List<Item> items) {
super(DealList.this, R.layout.list_item, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
// use ViewHolder here to prevent multiple calls to findViewById (if you have a large collection)
TextView text = (TextView) convertView.findViewById(R.id.deal_title);
ImageView image = (ImageView) convertView.findViewById(R.id.deal_img);
image.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ddicon));
Item item = getItem(position);
if (item != null) {
text.setText(item.getTitle());
Bitmap bitmap = app.getImageCache().get(item.getItemId()); //<------HERE
if (bitmap != null) {
image.setImageBitmap(bitmap);
} else {
// put item ID on image as TAG for use in task
image.setTag(item.getItemId());
// separate thread/via task, for retrieving each image
// (note that this is brittle as is, should stop all threads in onPause)
new RetrieveImageTask(image).execute(item.getSmallPicUrl());
}
}
return convertView;
}
}
RetriveImageTask
private class RetrieveImageTask extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
public RetrieveImageTask(ImageView imageView) {
this.imageView = imageView;
}
#Override
protected Bitmap doInBackground(String... args) {
Bitmap bitmap = app.retrieveBitmap(args[0]);
return bitmap;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
app.getImageCache().put((Long) imageView.getTag(), bitmap);
imageView.setTag(null);
}
}
}
Its called lazy loading. well images take time to download from net so by that some dummy image is set. As the downloading completes it will replaced with dummy image. Basically is matter of user experience with application.
the tag has something to do with how the cache mechanism of your code works- the key of the items is their number in this sample , meaning it is used to identify which image was downloaded so that you could load it from the cache instead of from the internet.
i agree that it's weird, as it could simply put the url of the image instead. using the url is more logical .
the sample isn't so efficient as it doesn't use the viewHolder design pattern (you can learn about it via the lecture "the world of listView") and doesn't have downsampling in mind (you can check out this post about it).
the image that is shown before showing the correct image is for showing the user that it's being prepared (like a placeholder saying "downloading...").
it's just a sample for you to learn from.
I want to store images in my database. Also I want to check that if the image and title is already in the database. If so, it will not add them to the database. This is my class.
Attractions
public class Attractions extends ListActivity {
DataBaseHandler db = new DataBaseHandler(this);
ArrayList<Contact> imageArry = new ArrayList<Contact>();
List<Contact> contacts;
ContactImageAdapter adapter;
int ctr, loaded;
int [] landmarkImages={R.drawable.oblation,R.drawable.eastwood,R.drawable.ecopark,R.drawable.circle};
String []landmarkDetails = { "Oblation", "Eastwood", "Ecopark", "QC Circle"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attractions);
ctr = db.checkContact(landmarkDetails[loaded]);
// get image from drawable
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
for(loaded=0; loaded <landmarkDetails.length;loaded++){
Bitmap image = BitmapFactory.decodeResource(getResources(),
landmarkImages[loaded]);
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
Log.d("Going to load images", "Image "+ loaded);
Log.d("Goind to load objects", "loading");
if(ctr == 0){
Log.d("Nothing Loaded", "Loading Now");
db.addContact(new Contact(landmarkDetails[loaded], imageInByte));}
Log.d(landmarkDetails[loaded], "Loaded!");
image.recycle();
}
loadFromDb();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.attractions, menu);
return true;
}
public void loadFromDb(){
// Reading all contacts from database
contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName()
+ " ,Image: " + cn.getImage();
// Writing Contacts to log
Log.d("Result: ", log);
//add contacts data in arrayList
imageArry.add(cn);
}
adapter = new ContactImageAdapter(this, R.layout.screen_list,
imageArry);
ListView dataList = (ListView) findViewById(android.R.id.list);
dataList.setAdapter(adapter);
}
public void onPause(){
super.onPause();
}
public void onResume(){
super.onResume();
}
}
It works fine on the emulator, but I tried testing on my S4 and then after 3 tries of going to this class, it forced stop. I tried it with usb debugging and the logcat showed java.lang.outofmemoryerror . The logcat pointed the error in my contactimageadapter.
ContactImageAdapter
public class ContactImageAdapter extends ArrayAdapter<Contact>{
Context context;
int layoutResourceId;
// BcardImage data[] = null;
ArrayList<Contact> data=new ArrayList<Contact>();
public ContactImageAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
Contact picture = data.get(position);
holder.txtTitle.setText(picture._name);
//convert byte to bitmap take from contact class
byte[] outImage=picture._image;
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgIcon.setImageBitmap(theImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
And pointed to this line Bitmap theImage = BitmapFactory.decodeStream(imageStream);
I have little (almost none) knowledge on managing images and storing them. I also enable android:largeHeap but still force closes on multiple tries. I hope someone can help me solving this issue, or at least show me a different way of storing text and images to sqlite db. Many thanks!
You have multiple places where whole image (assuming it is big) keeps in memory:
Contact object has it. All loaded images are in imageArry which is instance level variable.
public class Attractions extends ListActivity {
DataBaseHandler db = new DataBaseHandler(this);
ArrayList<Contact> imageArry = new ArrayList<Contact>();
in ContactImageAdapter.getView method you create another copy of image as BMP in holder object and pass it out of method.
So, at some point you do not have enough memory to keep all of them. Also I sure that decodeStream needs some more memory to perform.
After all it is not predictable when each new holder created in getView will be cleaned by GC.
Usually for such situation when object created as new in some method, then passed back to the calling method, that object will be collected only by Full GC.
So, as "Software Sainath" said, do not store images in database…
and do not keep them in memory either.
P.S. Then provide to the view a link to the external image file. That also will save time to load a view. Image will be in cache and if user at least once got it, it will not pass through the network again.
I guess images there are not frequently change them self. another image of Contact will be another file…
I wrote an answer to the somewhat similar problem some while ago, here is the link that you can check. The problem is in the approach of saving the images into the database, you should not be doing this. Instead, write the images as files on the phone memory and use it further.
Don't store Image to Sqlite Database eventually, you will ran into out of memory error after three or five image saved to database. It's not the best practice, maximum memory allocated for field in a row in sqlite is less than 3mb, be aware of this.
Instead of saving Images to database, Keep the images inside your app folder, save the path to the Database.
Your are loading your image as it is to your Image adapter. Let's say your image is 1280x720 resolution and 2mb in size, it will take the same space in your memory Heap.
You can either scaledown your image and load it as bitmap to your Adapter ImageView like this.
Before loading your image as Bitmap get it height and width.
//Code read the image and give you image height and width.it won't load your bitmap.
BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
BitmapFactory.decodeFile(your_image_url,option);
int image_original_width = option.outHeight;
int image_original_height = option.outWidth;
Now to scale down your Image you have to know the ImageView width and height. This is because we are going to scale down the image matching the imageview with pixel perfection.
int image_view_width = image_view.getWidht();
int image_view_height = image_view.getHeight();
int new_width;
int new_height;
float scaled_width;
if(image_original_width>image_view_width)
{ //if the image_view width is lesser than original_image_width ,you have to scaled down the image.
scale_value =(float)image_original_width/(float)image_view_width;
new_width = image_original_width/scaled_value;
new_height = image_orignal_height/scale_value
}
else
{
// use the image_view width and height as sacling value widht and height;
new_width = image_view_width;
new_height = image_view_height;
}
Now Scale Down your bitmap and load it like this.
// this will load a bitmap with 1/4 the size of the original one.
// this to lower your bitmap memory occupation in heap.
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 4;
Bitmap current_bitmap = BitmapFactory.decodeFile(image_url,option);
Bitmap scaled_bitmap = Bitmap.createScaledBitmap(current_bitmap,new_width,new_height,true);
holder.imgIcon.setImageBitmap(scaled_bitmap);
//release memory occupied by current_bitmap in heap, as we are no longer using it.
current_bitmap.recycle();
If you want to understand a little more about Bitmap and memory view this link.
If you don't want to handle rescaling bitmap by yourself. you can use Glide or Picasso library which does the same.
I have written an article about using Picasso to load image in listview, which will help you to start, if you are looking to use picasso.
http://codex2android.blogspot.in/2015/11/picasso-android-example.html
Please make sure to use the quick garbage collection eligible reference type while loading the images from the network
import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import android.graphics.Bitmap;
public class MemoryCache {
private Map<String, SoftReference<Bitmap>> cache=Collections.synchronizedMap(new HashMap<String, SoftReference<Bitmap>>());
public Bitmap get(String id){
if(!cache.containsKey(id))
return null;
SoftReference<Bitmap> ref=cache.get(id);
return ref.get();
}
public void put(String id, Bitmap bitmap){
cache.put(id, new SoftReference<Bitmap>(bitmap));
}
public void clear() {
cache.clear();
}
}
Don't store Image to Sqlite Database. It's not the best practice.
Instead of saving Images to database, Keep the images in a storage, but if you want to keep them private then keep them inside your app folder and save the path to the Database.
Use one of the well known libraries like http://square.github.io/picasso/ or https://github.com/bumptech/glide, they offer great help with memory issues and also some cool transition effects.
I recommend using Glide because it works very well on device with low memory restrictions
Blatant n00b question: I have several directories of pictures and wish to display randomly pictures from only one, which I select by a set of radio buttons. How do I specify the directory when using :
//"ha" is ha.png, which I would like to be at drawable/1/ha.png
image.setImageResource(R.drawable.ha);
Can I use setImageResource for this? If so how? If not, what should I use and how?
The object of the exercise is a flashcard program with different lessons (hence the dividing up of images) selectable at the first activity.
You cannot have subfolders under res/drawable, if you are referring to the drawables folder in your apk.
If you are referring to a random folder on your sdcard, then it's fine to use subfolders, but then you cannot use R.drawable.* for that approach to refer to the image.
In that case you need to load the image using
Bitmap bmp = BitmapFactory.decodeFile("/sdcard/drawable/1/ha.png");
which returns a bitmap, which you can use like
image.setImageBitmap(bmp)
see http://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap(android.graphics.Bitmap)
In order to react on changes made to the radion button, see
How to set On click listener on the Radio Button in android
You can use a GridView to show the images from a directory selected from a radio button (as your requirement says). After creating a GridView, associate a adapter to it. Please refer below for a n example adapter :
public class ImageAdapter extends BaseAdapter {
/** LayoutInflater. */
private LayoutInflater mInflater;
/** The i. */
private ImageView i;
/**
* Instantiates a new image adapter.
*
* #param c
* the c
*/
public ImageAdapter(Context c) {
mInflater = LayoutInflater.from(c);
}
public int getCount() {
// scaled pictures will have the list of
// which you have from the directory
return scaledPictures.size();
}
public Bitmap getItem(int position) {
return scaledPictures.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.image, parent, false);
} else {
i = (ImageView) convertView;
}
Bitmap bitmap = getItem(position);
i = (ImageView) convertView.findViewById(R.id.galleryimage);
i.setImageBitmap(bitmap);
bitmap = null;
return i;
}
}
I'm trying to download images for each artist that has music on my phone, then show these images in a GridView. I'm using the lastfm-java library that Last.fm recommends using. The method you call to fetch an artists image is getImageURL(ImageSize size), but before you do this, you need to tell it which artist you want to reference with a String parameter. So, in full it would be something like this:
#Override
protected String doInBackground(Object... arg0) {
Artist artist = Artist.getInfo(artistOrMbid, LASTFM_API_KEY);
return artist.getImageURL(ImageSize.EXTRALARGE);
}
Getting all the artists that are on my phone isn't a problem, you just reference MediaStore. You would do something like this:
private void getArtists() {
String[] projection = new String[] {
MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST,
};
String sortOrder = MediaStore.Audio.Artists.DEFAULT_SORT_ORDER;
Cursor c = getActivity().getContentResolver().query(
MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, projection, null, null, sortOrder);
if (c != null) {
int count = c.getCount();
if (count > 0) {
final int ARTIST_IDX = c.getColumnIndex(MediaStore.Audio.Artists.ARTIST);
for (int i = 0; i < count; i++) {
c.moveToPosition(i);
}
}
c.close();
c = null;
}
}
The Adapter for my GridView isn't anything special, it simply extends BaseAdapter.
Note AQuery is a library I'm using that helps cache and load a Bitmap from a URL.
public class GridViewAdapter extends BaseAdapter {
private final String[] imageURLs;
private final LayoutInflater mInflater;
private final Activity mActivity;
public GridViewAdapter(String[] urls, Activity activity) {
imageURLs = urls;
mActivity = activity;
mInflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return imageURLs.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewholder = null;
// Inflate GridView items
if (convertView == null) {
convertView = mInflater.inflate(R.layout.gridview_items, null);
viewholder = new ViewHolder();
viewholder.mImage = (ImageView)convertView.findViewById(R.id.gridview_image);
convertView.setTag(viewholder);
} else {
viewholder = (ViewHolder)convertView.getTag();
}
AQuery aq = new AQuery(convertView);
aq.id(viewholder.mImage).image(imageURLs[position], false, false, 0, 0, null, 0, 0.75f);
return convertView;
}
}
class ViewHolder {
public ImageView mImage;
}
So in full, my AsyncTask is as follows:
public class LastfmArtistGetImageURL extends AsyncTask<Object, Integer, String[]> implements
Constants {
private static final String tag = LastfmArtistGetImageURL.class.getSimpleName();
private GridViewAdapter mGridAdapter;
// Test
private final String[] imageIds = {
"http://userserve-ak.last.fm/serve/252/71875544.png",
"http://userserve-ak.last.fm/serve/252/6258507.jpg",
"http://userserve-ak.last.fm/serve/252/51274303.png",
"http://userserve-ak.last.fm/serve/252/58672183.png",
"http://userserve-ak.last.fm/serve/252/72029714.png",
"http://userserve-ak.last.fm/serve/252/17666215.jpg",
"http://userserve-ak.last.fm/serve/252/63247381.png",
"http://userserve-ak.last.fm/serve/252/33665463.jpg"
};
private final String artistOrMbid;
private final GridView mGridView;
private final Activity mActivity;
public LastfmArtistGetImageURL(String name, GridView gv, Activity activity) {
artistOrMbid = name;
mGridView = gv;
mActivity = activity;
}
#Override
protected String[] doInBackground(Object... arg0) {
Artist artist = Artist.getInfo(artistOrMbid, LASTFM_API_KEY);
Collection<String> col = new ArrayList<String>();
col.add(artist.getImageURL(ImageSize.EXTRALARGE));
return col.toArray(new String[0]);
}
#Override
protected void onPostExecute(String[] result) {
if (result != null)
mGridAdapter = new GridViewAdapter(imageIds, mActivity);
mGridView.setAdapter(mGridAdapter);
super.onPostExecute(result);
}
}
When I call my AsyncTask, I call it in my getArtists() method like this:
new LastfmArtistGetImageURL(c.getString(ARTIST_IDX), mGridView, getActivity())
.execute();
Problem
When I call this, all of the artists images download, but they download one after the other at position 0 of my GridViewAdapter. In other words, one image loads, then next, and so on all in the first position when I need them to be placed into each available position in the GridView. When I return my test String[] in my AsyncTask everything works like it should. All of the images are placed in order in each available space in the GridView.
Question
My question is, how do I return each artist image I download into my GridView correctly and why are the images currently only being loaded at the first position in my GridViewAdapter?
Edit - Shubhayu's answer
I moved setting my GridViewAdapter into my getArtists() method like so. This results in all the images being downloaded (As says LogCat), but only the last one being set in my GridView.
String[] test = new LastfmArtistGetImageURL(c.getString(ARTIST_IDX),
mGridView, getActivity()).execute().get();
mGridAdapter = new GridViewAdapter(test, getActivity());
mGridView.setAdapter(mGridAdapter);
smoak's answer
This results in only the last artist image (by the default order) being downloaded and applied in my GridView.
String[] test = {c.getString(ARTIST_IDX)};
new LastfmArtistGetImageURL(test, mGridView, getActivity()).execute();
Your AsyncTask looks like you are executing it each time for each Artist. Thus, your AsyncTask returns only one Artist's image and your GridView gets that Artists image, then you run the AsyncTask for the next Artist, GridView gets updated with new image and so on. What you need to do is modify your AsyncTask to take a String array of Artist names and loop over them in the doInBackground to get their image's.
// ... SNIPPED
public LastfmArtistGetImageURL(String[] names, GridView gv, Activity activity) {
artistsOrMbids = names;
mGridView = gv;
mActivity = activity;
}
#Override
protected String[] doInBackground(Object... arg0) {
Collection<String> col = new ArrayList<String>();
for (String nameOrMbid : this.artistsOrMbids) {
Artist artist = Artist.getInfo(artistOrMbid, LASTFM_API_KEY);
col.add(artist.getImageURL(ImageSize.EXTRALARGE));
}
return col.toArray(new String[0]);
}
// .... SNIPPED
And pass in all the artist names:
String[] artists = { "The Black Keys", "Rush", "The Allman Brothers" };
new LastfmArtistGetImageURL(artists, mGridView, getActivity()).execute();
here's what is happening, when you pass the test string it has a list of images and hence the gridview shows them properly. but when you use it to download an image for each artist, things go wrong.
Every time you call
new LastfmArtistGetImageURL(c.getString(ARTIST_IDX), mGridView, getActivity()).execute();
it runs the doInBackground(), completes it and then immediately calls the onPostExecute() where it creates a new adapter and passes your result which basically contains a single image of the single call.
So what u need to do is in your asynctask download all the images and then create a single adapter and pass all the images to it. That is not happening currently.
EDIT
If you see the AsyncTask, you will realize that everytime you call it, the string array returns only one image. So instead of returning a string array, return a string.
Next, I would suggest you use an ArrayList in your Adapter instead of a String array.
In your getArtists(), create an ArrayList and everytime you call
new LastfmArtistGetImageURL(test, mGridView, getActivity()).execute();
add the result to your ArrayList. Once you have looped through all the artists, your ArrayList will contain all the images.
Now set it to the Adapter. (You would have t change the adapter a bit if you change it from string to arraylist.)