I have been going over the logic all night, and just cannot get an answer. I need for my drawables placeholder images, to not be clickable, and for my real images (photos from an SD card) to all be clickable for both regular clicks and long clicks. I have logged a million versions of how to write an if condition that would say something like: if the image Bitmap is the drawable and equal to the Bitmap in the position clicked on, then process the click listeners. But this can never happen! They are not ever equal. The Bitmap numbers always come up as different (which makes no sense to me), when I compare the drawable bitmap with the bitmap at the position clicked.
So I have tried another approach. I want to try what I found here to directly disable the drawable at its position. These methods, I put in my adapter class.
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return true;
}
But the methods make no sense to me. Where/how do I call them in my situation? I don't understand how these are supposed to disable select items in my custom GridView. If you have any ideas, thanks.
Here is my log statement values from logcat. I got these by clicking on the drawable image in my GridView, but as you can see, the image Bitmap and the adapter position item are not the same. So how can I identify my drawable so I know to disable the click on there?
Value of adapter position item: android.graphics.Bitmap#430706e0
Value of drawableObject: android.graphics.Bitmap#4301fff0
Value of photoGridItem: android.graphics.Bitmap#4301a768
PhotoTab.java
package org.azurespot.cutecollection.phototab;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import org.azurespot.R;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
/**
* Created by mizu on 2/8/15.
*/
public class PhotoTab extends Fragment {
private GridView gridView;
File[] files;
ArrayList<PhotoGridItem> photoList = new ArrayList<>();
ArrayAdapter<PhotoGridItem> adapter;
Bitmap bitmap;
private String[] numberSDCardFiles = null;
PhotoGridItem drawableObject;
PhotoGridItem photoGridItem;
public PhotoTab() {
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.photo_tab, container, false);
adapter = new GridViewPhotoAdapter(getActivity(), photoList);
// with fragments, make sure you include the rootView when finding id
gridView = (GridView) v.findViewById(R.id.photo_grid);
gridView.setAdapter(adapter);
if(adapter.getCount() == 0) {
// load contents of SD card
loadSDCard();
// add the default icons remaining, to GridView, if less than 24 files on SD card
for (int i = 0; i < (24 - numberSDCardFiles.length); i++) {
drawableObject = new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder));
adapter.add(drawableObject);
adapter.notifyDataSetChanged();
}
}
setupGridViewListener();
return v;
}
public void loadSDCard() {
try {
// gets directory CutePhotos from sd card
File cutePhotosDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), "Cute Photos");
// lists all files in CutePhotos, loads in Files[] array
files = cutePhotosDir.listFiles();
for (File singleFile : files) {
String filePath = singleFile.getAbsolutePath();
// this method makes size small for the view (to save memory)
bitmap = decodeImageBitmap(filePath, 270, 270);
photoGridItem = new PhotoGridItem(bitmap);
// Check if this is a new bitmap file
adapter.add(photoGridItem);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
// get number of files in Cute Photos directory
numberSDCardFiles = new String[files.length];
}
private void setupGridViewListener(){
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView,
View item, int pos, long id) {
//Convert the bitmap to byte array, so can pass through intent
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bm = adapter.getItem(pos).getImage();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] byteArray = stream.toByteArray();
Intent i = new Intent(getActivity(), PhotoViewerActivity.class);
i.putExtra("photo", byteArray);
startActivity(i);
Log.d("TAG", "Value of bm: " + bm);
Log.d("TAG", "Value of adapter position item: " + adapter.getItem(pos));
Log.d("TAG", "Value of drawableObject: " + drawableObject);
}
});
// to delete a photo item
gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> aView, View item,
final int pos, long id) {
new AlertDialog.Builder(getActivity())
.setTitle("Delete")
.setMessage("Delete this cute photo?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// delete from ArrayList first
photoList.remove(pos);
// get file name then delete it from SD card
String name = files[pos].getName();
File file = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), "Cute Photos" + "/" + name);
file.delete();
// after each item delete, replace with default icon
adapter.add(new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder)));
adapter.notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
dialog.cancel();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
return true;
}
});
}
// next 2 methods scale the bitmap image to a better size (so not huge)
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public static Bitmap decodeImageBitmap(String path, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(path, options);
return bm;
}
}
GridViewPhotoAdapter.java
package org.azurespot.cutecollection.phototab;
/**
* Created by mizu on 2/5/15.
*/
// package org.azurespot.cutecollection;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import org.azurespot.R;
import java.util.ArrayList;
/**
* Created by mizu on 2/5/15.
*/
public class GridViewPhotoAdapter extends ArrayAdapter<PhotoGridItem> {
ViewHolder holder = null;
int position;
public GridViewPhotoAdapter(Context context, ArrayList<PhotoGridItem> photos) {
super(context, 0, photos);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
this.position = position;
if (v == null) {
v = LayoutInflater.from(getContext())
.inflate(R.layout.photo_grid_item, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) v.findViewById(R.id.photo_grid_view);
// stores holder with view
v.setTag(holder);
} else {
holder = (ViewHolder)v.getTag();
}
// gets position of whichever photo you click on in the GridView
final PhotoGridItem photoGridItem = getItem(position);
if (photoGridItem != null) {
Bitmap bm = photoGridItem.getImage();
holder.imageView.setImageBitmap(bm);
// positioning the image in the GridView slot
holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(270, 270));
}
return v;
}
public class ViewHolder{
ImageView imageView;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return true;
}
}
Add your logic inside IsEnabled method like below
#Override
public boolean isEnabled(int position) {
if(position == POSITION_U_WANT_TO_DISABLE) {
return false;
}
return true;
}
According to your code you always returning true (Enabled) inside that method.
Unbelievable, but I found the right logic after all. I did not need the 2 List methods mentioned above, since those relied upon me identifying a drawable versus a photo in my GridView positioning. So at the time of adding my placeholder drawable to my grid, I also put them all into an ArrayList<Bitmap>, so that I could search the list and pull out drawable values when needed.
So then I added a conditional to the click listener's code, that said if my ArrayList of Bitmaps contains the bitmap found at the position of the view that was clicked, then do not proceed with executing the click listener's code. If it does not contain a Bitmap from the ArrayList, then go ahead and process it.
I checked if I could still add and delete items from the GridView and still have that conditional work, and I sure could. Below are the new additions in context.
ArrayList<Bitmap> bmList = new ArrayList<>();
...
// add the default icons remaining, to GridView, if less than 24 files on SD card
for (int i = 0; i < (24 - numberSDCardFiles.length); i++) {
drawableObject = new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder));
// adds each drawable Bitmap to an ArrayList
bmList.add(drawableObject.getImage());
// adds to adapter
adapter.add(drawableObject);
adapter.notifyDataSetChanged();
}
...
private void setupGridViewListener(){
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView,
View item, int pos, long id) {
if(!bmList.contains(adapter.getItem(pos).getImage())) {
//Convert the bitmap to byte array, so can pass through intent
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bm = adapter.getItem(pos).getImage();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] byteArray = stream.toByteArray();
Intent i = new Intent(getActivity(), PhotoViewerActivity.class);
i.putExtra("photo", byteArray);
startActivity(i);
}
}
});
Related
I'm using Firebase to download pictures from the server and display them in a listview in Android Studio. Specifically, it's a list of pictures that I store in an array which I later use for the listview.
If I need to download just one image the code works just fine, the problem is that when I receive more than one the listview displays only blank images with the exception of the last one, which is the right one for that spot. I've already used that listview for other stuff and I know it works.
Here is my method:
public void fetchImages(Map<String,Object> bitmaps) {
for (Map.Entry<String, Object> entry : bitmaps.entrySet()) {
storageRef = storage.getReference().child(entry.getKey());
try {
localFile = File.createTempFile("FileTemp", "jpg");
storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
returnedBitmaps[i++] = BitmapFactory.decodeFile(localFile.getAbsolutePath());
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
}
});
} catch(Exception e) {
}
}
}
I know that I always get a number of pictures equal to the array dimension so I don't get errors in that part. If I try taskSnapshot.getBytesTransferred() I get the exact number of bytes the pictures from the server have so I think the pictures are there and i'm just doing something wrong when I'm trying to display them. Any help would be appreciated, thanks.
Edit: this is the method I'm using on my listview:
public void fixImages () {
listItemsName = new String[returnedContacts.length];
imageNameBitmap = new Bitmap[returnedContacts.length];
int count = 0;
while (count < returnedContacts.length) {
listItemsName[count] = returnedContacts[count].name + " " + returnedContacts[count].lastName;
imageNameBitmap[count] = returnedBitmaps[count];
count++;
}
listView = (ListView)findViewById(R.id.listView1);
try {
listAdapterBitmap = new ListAdapterBitmap(CheckFollows.this, listItemsName, imageNameBitmap);
}
catch (Exception e) {
e.printStackTrace();
}
listView.setAdapter(listAdapterBitmap);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(returnedContacts[position] != null) {
Intent intent = new Intent(CheckFollows.this, PersonBrowsed.class);
startActivity(intent);
}
}
});
}
The listview displays on the right side a name and a last name using the listItemName array while showing a picture on the left side, using the imageNameBitmap array.
Class ListAdapterBitmap:
import android.app.Activity;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
class ListAdapterBitmap extends ArrayAdapter<String> {
private final Activity Context;
private final String [] ListItemsName;
private final Bitmap [] ImageName;
ListAdapterBitmap(Activity context, String [] content, Bitmap [] ImageName) {
super(context, R.layout.list_items, content);
this.Context = context;
this.ListItemsName = content;
this.ImageName = ImageName;
}
#NonNull
public View getView(int position, View view,#NonNull ViewGroup parent) {
LayoutInflater inflater = Context.getLayoutInflater();
View ListViewSingle = inflater.inflate(R.layout.list_items, null, true);
TextView ListViewItems = (TextView)ListViewSingle.findViewById(R.id.tvSportName);
ImageView ListViewImage = (ImageView)ListViewSingle.findViewById(R.id.imgSportPicture);
ListViewItems.setText(ListItemsName[position]);
ListViewImage.setImageBitmap(ImageName[position]);
return ListViewSingle;
}
}
I am trying to learn to create a meme generator app. First I had a problem with my app crashing when I clicked on the images in the grid view as the images were too big. I was advised to use image id to pass the image to the second activity. I (thought, I) changed the code accordingly. Now when clicked on the image, I can see the next activity with enter top and bottom text options but still image does not appear. I know the problem is the way I am passing image id but don't know what. I hope you can give me specific code as I am very new to programming.
I know using uri also is an option but being a beginner I am not sure how I would execute that here. If you think it is a better (in terms of speed of the app and memory usage / easier to learn and execute), you would be kind enough to help me out with the code too, would be appreciated.
Thank You in advance!
Here is my code:
Main Activity.java
package com.javatechig.gridviewexample;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private GridView gridView;
private GridViewAdapter gridAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
gridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());
gridView.setAdapter(gridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageItem item = (ImageItem) parent.getItemAtPosition(position);
//Create intent
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
intent.putExtra("id", item.getId());
//Start details activity
startActivity(intent);
}
});
}
/**
* Prepare some dummy data for gridview
*/
private ArrayList<ImageItem> getData() {
final ArrayList<ImageItem> imageItems = new ArrayList<>();
TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgs.getResourceId(i, -1));
imageItems.add(new ImageItem(bitmap, R.array.image_ids));
}
return imageItems;
}
}
DetailsActivity.java
package com.javatechig.gridviewexample;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
public class DetailsActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_activity);
//imageUri = Uri.parse(extras.getString("imageUri"));
Bitmap bitmap = getIntent().getParcelableExtra("id");
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(bitmap);
}
}
ImageItem.java
package com.javatechig.gridviewexample;
import android.graphics.Bitmap;
public class ImageItem {
private Bitmap image;
private int id;
public ImageItem(Bitmap image, int id) {
super();
this.image = image;
this.id = id;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
}
GridViewAdapter.java
package com.javatechig.gridviewexample;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import java.util.ArrayList;
public class GridViewAdapter extends ArrayAdapter<ImageItem> {
private Context context;
private int layoutResourceId;
private ArrayList<ImageItem> data = new ArrayList<ImageItem>();
public GridViewAdapter(Context context, int layoutResourceId, ArrayList<ImageItem> 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;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.image.setImageBitmap(item.getImage());
return row;
}
static class ViewHolder {
ImageView image;
}
}
There are a few issues with your code. To start, you are obtaining your images from the resources but when you create a new ImageItem and add it to the ArrayList you are not passing in the individual resource ID. Here is the relevant part of your getData() method:
TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgs.getResourceId(i, -1));
//Here you are putting in the generic R.array.image_ids
//Instead you should put in the images specific id: imgs.getResourceId(i, -1));
imageItems.add(new ImageItem(bitmap, R.array.image_ids));
}
Next, you are incorrectly passing and retrieving data between Activities. Here's your relevant code:
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
intent.putExtra("id", item.getId()); //You are putting in an int extra
...
//You are attempting to retrieve a Parcelable extra even though you only
//passed in an int
Bitmap bitmap = getIntent().getParcelableExtra("id");
And finally, I want to make it clear that you are not storing these ImageItems anywhere, so, you wouldn't be able to retrieve the correct ImageItem with the ID to get the image.
So, what's the solution?
As you may know Bitmaps can be quite large and passing them between Activities isn't the best approach as there can be many issues in doing so. One way of getting around this is storing them in a File and retrieving that same File in the other Activity. Fortunately, all of the Bitmaps you need are already stored in your resources, so, you can just get them from there.
First, fix your getData() method:
private ArrayList<ImageItem> getData() {
final ArrayList<ImageItem> imageItems = new ArrayList<>();
TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
try{
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgs.getResourceId(i, -1));
imageItems.add(new ImageItem(bitmap, imgs.getResourceId(i, -1)));
}
}finally{
imgs.recycle(); //When done recycle your TypedArray
}
return imageItems;
}
Then, your onCreate() method in DetailsActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_activity);
Intent intent = getIntent();
int id = intent.getIntExtra("id", -1);
if(id != -1){
//We have the resource ID so we can retreive the item
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id);
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(bitmap);
}else{
//We don't have the resource ID so show default View or perform task
}
}
item.getId() is the unique ID in memory , not the image's resource ID, you should use resource ID.
the simple way , you can set resource ID as imageItems ID in setgetData().
I have tried all the suggestions I could find on S.O., and I was able to use this exact code successfully on a ListView of mine, but for some reason I can't set a long click listener on my GridView. I log inside the method setupGridViewListener(); and it does not even run, yet I have my method inside onCreateView(). Don't know why it won't work. The alert dialog should pop up once long-clicked, but it doesn't. Method is at bottom of my class.
UPDATE:
I did not mention that my onClickListener does work fine. So I can at least do a single click, and this displays my activity. But, that code is in my adapter, so maybe they should be together in the same class? I am including my adapter and my 2 xmls below.
UPDATE 2
I discovered several things that were worth mentioning, even after I got my code working again. First of all, putting my click listeners into my adapter was a mistake. It slowed things down HUGELY... so much so, I was frantic about finding a way to get things on another thread. But in the end, I did not need to. I realized that I could actually get both of my click listeners into my PhotoTab.java class. This immediately made it faster (back to normal). But another curious thing, the IDE would not let me use OnClickListener, but insisted I go back to OnItemClickListener. So once I changed those back, everything worked again. So the listeners ended up being on my GridView variables, e.g. gridView.setOnItemClickListener(new OnItemClickListener() {} not my ImageView item variables. Perhaps this is what made things faster? There is one GridView, but 24 ImageViews (GridView cells). But that is just a guess. I have updated my code to the current, working code. Except I'm still working on the long click code, so that's not right yet. The dialog alert works though, and each item in the GridView is clickable.
PhotoTab.java
package org.azurespot.cutecollection.phototab;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import org.azurespot.R;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
/**
* Created by mizu on 2/8/15.
*/
public class PhotoTab extends Fragment {
private GridView gridView;
File[] files;
ArrayList<PhotoGridItem> photoList = new ArrayList<>();
ArrayAdapter<PhotoGridItem> adapter;
Bitmap bitmap;
byte[] byteArray = null;
private String[] allSDCardFiles = null;
PhotoGridItem photoGridItem;
public PhotoTab() {
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.photo_tab, container, false);
// with fragments, make sure you include the rootView when finding id
gridView = (GridView) v.findViewById(R.id.photo_grid);
// this null check won't instantiate again if it was already
// if(adapter == null)
adapter = new GridViewPhotoAdapter(getActivity(), R.layout.photo_grid_item);
// Set the Adapter to GridView
gridView.setAdapter(adapter);
// load contents of SD card
loadSDCard();
// add the default icons remaining, to GridView, if less than 24 files on SD card
for (int i = 0; i < (24 - allSDCardFiles.length); i++) {
adapter.add(new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder)));
adapter.notifyDataSetChanged();
i++;
}
//Convert the bitmap to byte array, so can pass through intent
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] byteArray = stream.toByteArray();
this.byteArray = byteArray;
setupGridViewListener();
return v;
}
public void loadSDCard() {
try {
// gets directory CutePhotos from sd card
File cutePhotosDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), "Cute Photos");
// lists all files in CutePhotos, loads in Files[] array
files = cutePhotosDir.listFiles();
for (File singleFile : files) {
String filePath = singleFile.getAbsolutePath();
// this method makes size small for the view (to save memory)
bitmap = decodeImageBitmap(filePath, 270, 270);
photoGridItem = new PhotoGridItem(bitmap);
// Check if this is a new bitmap file
adapter.add(photoGridItem);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
// get number of files in Cute Photos directory
allSDCardFiles = new String[files.length];
}
private void setupGridViewListener(){
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter,
View item, int pos, long id) {
Intent i = new Intent(getActivity(), PhotoViewerActivity.class);
i.putExtra("photo", byteArray);
startActivity(i);
}
});
// to delete a photo item
gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> aView, View item,
final int pos, long id) {
new AlertDialog.Builder(getActivity())
.setTitle("Delete")
.setMessage("Delete this cute photo?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// delete from ArrayList first
// PhotoTab.photoList.remove(position);
// get file name then delete it from SD card
File cutePhotosDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), "CutePhotos/" + photoGridItem);
cutePhotosDir.delete();
// after each item delete, must refresh load so can delete again
// photoTab.loadSDCard();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
dialog.cancel();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
return true;
}
});
}
// next 2 methods scale the bitmap image to a better size (so not huge)
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public static Bitmap decodeImageBitmap(String path, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(path, options);
return bm;
}
}
GridViewPhotoAdapter
package org.azurespot.cutecollection.phototab;
/**
* Created by mizu on 2/5/15.
*/
// package org.azurespot.cutecollection;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import org.azurespot.R;
/**
* Created by mizu on 2/5/15.
*/
public class GridViewPhotoAdapter extends ArrayAdapter<PhotoGridItem> {
public Context context;
private int resourceId;
ViewHolder holder = null;
int position;
public GridViewPhotoAdapter(Context context, int layoutResourceId) {
super(context, layoutResourceId);
this.context = context;
this.resourceId = layoutResourceId;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
this.position = position;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) row.findViewById(R.id.photo_grid_view);
// stores holder with view
row.setTag(holder);
} else {
holder = (ViewHolder)row.getTag();
}
// gets position of whichever photo you click on in the GridView
final PhotoGridItem photoGridItem = getItem(position);
if (photoGridItem != null) {
Bitmap bm = photoGridItem.getImage();
holder.imageView.setImageBitmap(bm);
// positioning the image in the GridView slot
holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(270, 270));
}
return row;
}
public class ViewHolder{
ImageView imageView;
}
}
photo_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_view"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2198bb">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/photo_grid"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:verticalSpacing="5dp"
android:horizontalSpacing="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="25dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:scrollbarStyle="outsideOverlay"
android:verticalScrollbarPosition="right" />
</RelativeLayout>
photo_grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/photo_grid_view"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
package com.example.slideanim;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;
public class MyGridAdapter extends BaseAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Activity activity;
private LayoutInflater layoutInflater;
public MyGridAdapter(Activity activity, ArrayList<String> list) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.list = list;
layoutInflater = activity.getLayoutInflater();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int pos, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.row, null);
holder.imageView = (ImageView) convertView
.findViewById(R.id.photo_grid_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
holder.imageView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(activity, "got", Toast.LENGTH_SHORT).show();
return true;
}
});
return convertView;
}
private static class ViewHolder {
public ImageView imageView;
}
}
It would be better to either use click and long click for the imageview, or both on the gridview item.
If you want on imageview, do not setup the GridViewListener and try this in your adapter:
holder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//your code
return true;
}
});
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, PhotoViewerActivity.class);
i.putExtra("photo", byteArray);
context.startActivity(i);
}
});
-First of all check if any Views In your gridview are Focusable or Clickable or not make them False and then check your log again
-Let me inform if further issue exist
-THanks
I have been researching this a while, but nothing. My ArrayAdapter<> for my GridView is in a Fragment tab. I have concluded that because it is in a fragment, that when I browse tabs then go back to my photos tab, instead of re-uploading images from scratch, it instead uploads images and add the same ones to the adapter. This results in 2 sets of identical images, but what I need is only a single set to upload upon revisiting the tab.
I tried putting adapter.clear() followed by adapter.notifyDataSetChanged() into my onPause() fragment lifecycle method. So when the fragment is not visible anymore, all adapter items clear, and when the fragment is visible again, it loads a fresh copy of images from my SD card. But this is not working, it still adds the new set of images in addition to the previously loaded set. Anyone know how to get the adapter cleared so I don't get the same images added every time I revisit the tab? Or if there is another way to do it, that I'm not aware of ... Thanks in advance.
UPDATE:
Thanks to everyone for the contributions, I finally found a work around that helped. I logged all of my adapter variables at different stages, and found that no matter where I clear it, it will always post a duplicate set in the loadSDCard() method, so it must have been coming from there. So I just made a condition on my adapter to prevent it from loading again. I noticed that my adapter was already loaded after coming back from the other tabs, by using getCount() on it. This came from my PhotoTab.java and is the missing piece:
if (adapter.getCount() == 0) {
adapter.addAll(loadSDCard());
// add the default icons remaining, to GridView, if less than 24 files on SD card
for (int i = 0; i < (24 - photoList.size()); i++) {
adapter.add(new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder)));
}
}
UPDATE 2:
See my answer below, I found out the real reason why the duplicates were happening in the first place.
PhotoTab.java
package org.azurespot.cutecollection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import org.azurespot.R;
import java.io.File;
import java.util.ArrayList;
/**
* Created by mizu on 2/8/15.
*/
public class PhotoTab extends Fragment {
private GridView gridView;
File[] files;
ArrayList<PhotoGridItem> photoList = new ArrayList<>();
ArrayAdapter<PhotoGridItem> adapter;
Bitmap bitmap;
public PhotoTab() {
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.photo_tab, container, false);
// with fragments, make sure you include the rootView when finding id
gridView = (GridView) v.findViewById(R.id.photo_grid);
adapter = new GridViewPhotoAdapter(getActivity(), R.layout.photo_grid_item);
// Set the Adapter to GridView
gridView.setAdapter(adapter);
adapter.addAll(loadSDCard());
// add the default icons remaining, to GridView, if less than 24 files on SD card
for (int i = 0; i < (24 - photoList.size()); i++) {
adapter.add(new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder)));
}
return v;
}
private ArrayList<PhotoGridItem> loadSDCard() {
try {
// gets directory CutePhotos from sd card
File cutePhotosDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), "CutePhotos");
// lists all files in CutePhotos, loads in Files[] array
files = cutePhotosDir.listFiles();
for (File singleFile : files) {
String filePath = singleFile.getAbsolutePath();
// this method makes size small for the view (to save memory)
bitmap = decodeSampledBitmap(filePath, 270, 270);
photoList.add(new PhotoGridItem(bitmap));
}
} catch (Exception e) {
e.printStackTrace();
}
return photoList;
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmap(String path, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
#Override
public void onPause(){
super.onPause();
adapter.clear();
adapter.notifyDataSetChanged();
}
}
GridViewPhotoAdapter
package org.azurespot.cutecollection;
/**
* Created by mizu on 2/5/15.
*/
// package org.azurespot.cutecollection;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import org.azurespot.R;
/**
* Created by mizu on 2/5/15.
*/
public class GridViewPhotoAdapter extends ArrayAdapter<PhotoGridItem> {
public Context context;
private int resourceId;
Bitmap bm;
public GridViewPhotoAdapter(Context context, int layoutResourceId) {
super(context, layoutResourceId);
this.context = context;
this.resourceId = layoutResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) row.findViewById(R.id.photo_grid_view);
// stores holder with view
row.setTag(holder);
} else {
holder = (ViewHolder)row.getTag();
}
PhotoGridItem photoGridItem = getItem(position);
if (photoGridItem != null) {
bm = photoGridItem.getImage();
holder.imageView.setImageBitmap(bm);
// positioning the image in the GridView slot
holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(270, 270));
}
return row;
}
public class ViewHolder{
ImageView imageView;
}
}
PhotoGridItem
package org.azurespot.cutecollection;
import android.graphics.Bitmap;
/**
* Created by mizu on 3/19/15.
*/
public class PhotoGridItem {
private Bitmap image;
public PhotoGridItem(Bitmap image) {
super();
this.image = image;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
}
Fragment.onPause() isn't tied to the visibility of your Fragment. Fragment.onPause() is tied to it's encapsulating Activity.onPause(). i.e. Fragment.onPause() will only be called after Activity.onPause() is called.
If you want to update your Fragment when it becomes visible, I recommend you use Fragment.setUserVisibleHint() OR you use a ViewPager.onPageChangeListener() to let you know when your Fragment has been selected and update its contents accordingly.
Maybe you could try this modified your codes in onCreateView(...):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.photo_tab, container, false);
// with fragments, make sure you include the rootView when finding id
gridView = (GridView) v.findViewById(R.id.photo_grid);
if(adapter == null)
adapter = new GridViewPhotoAdapter(getActivity(), R.layout.photo_grid_item);
// Set the Adapter to GridView
gridView.setAdapter(adapter);
if(adapter.getCount()>0)
adapter.clear();
adapter.addAll(loadSDCard());
// add the default icons remaining, to GridView, if less than 24 files on SD card
for (int i = 0; i < (24 - photoList.size()); i++) {
adapter.add(new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder)));
}
adapter.notifyDataSetChanged(); //that was missing!!!
return v;
}
Hope this help!
In your fragment, in onCreateView, try checking whether savedInstanceState is null like this:
if (savedInstanceState == null) {
//Add all your images to the adapter
}
That way you should be sure that images are only added to the adapter when the view of the fragment is loaded the first time.
The onCreateView of PhotoTab fragment is triggered several times, like when user switches apps (Android may need to unload your app to gain memory) and when rotate the screen. So from your code, I think they could all be duplicates within time. I think your design is such that the reloading of images is done in the onCreateView method. One disadvantage may be performance, but let's not worry about that for now.
My suggestion is to modify loadSDCard() to check if a file is already processed or displayed. In your code sample:
private ArrayList<PhotoGridItem> loadSDCard() {
...
for (File singleFile : files) {
...
// Check if this is a new bitmap file
if (photoList.contains(bitmap) == false) {
photoList.add(new PhotoGridItem(bitmap));
}
}
}
While I found a work around for the duplicate problem, the real reason I was getting duplicates is because I made my loadSDCard() method return an ArrayList<>. I realized I did not need to do that. So when I changed my method to void instead, I could refresh the tab and the SD Card would load, but it would start from scratch each time, and load the photo items over the old items, without duplicates. I don't really know why returning an ArrayList<> variable (I was returning photoList) in that method made it do that, but hopefully someone else will be helped if you get duplicates in the future.
I downloaded the page curl project in github to apply it on my project using gridview. Once I click the image in gridview it will go to another activity in which there are arrays of image which I will pass the id from my gridview to my fullview activity using bundle. I want to use the page curl animation to slide between image in fullview so that the use will not have to go back to gridview just to see another image. But I can't pass an Integer to an ARRAYLIST. Here are the snippets:
This is my pagecurl adapter in this link: http://pastebin.com/JCq2YD6r
and my Base adapter for my gridview is here:
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public Integer[] mThumbIds = {
R.drawable.r01,R.drawable.r02
,R.drawable.r03,R.drawable.r04,
R.drawable.r05,R.drawable.r06,
R.drawable.r07,R.drawable.r08,
R.drawable.r09,R.drawable.r10,
R.drawable.r11,R.drawable.r12
};
public ImageAdapter(Context c){
mContext = c;
}
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(MainTab.current_act);
int dpw = app_preferences.getInt("dpiw", 0);
Log.i("IMmmm" , "" + dpw);
// if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mThumbIds[position]);
// int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, parent.getResources().getDisplayMetrics());
// int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 175, parent.getResources().getDisplayMetrics());
final float scale = parent.getResources().getDisplayMetrics().density;
int width = (int) (100 * scale + 0.5f);
int height = (int) (150 * scale + 0.5f);
Log.i("with h " , ""+width + " " + height + " scale " + scale );
imageView.setLayoutParams(new GridView.LayoutParams(width, height));
//}
// } else {
// imageView = (ImageView) convertView;
// }
return imageView;
}
}
This is the part i want to change in page curl adapter
// Create pages
mPages = new ArrayList<Bitmap>();
mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
How can I pass the Array of Integer to this arraylist the is bitmap? And by the way what do you call this? Arraylist? Is that arraylist of a bitmap?? :)
Check this example: http://devappandroid.com/android-beginner/page-curlturn-effect-android
// Create pages
mPages = new ArrayList<Bitmap>();
mPages.add(BitmapFactory.decodeResource(getResources(),
R.drawable.page1));
mPages.add(BitmapFactory.decodeResource(getResources(),
R.drawable.page2));
mPages.add(BitmapFactory.decodeResource(getResources(),
R.drawable.page3));
mPages.add(BitmapFactory.decodeResource(getResources(),
R.drawable.page4));