I created a gridview that show images from specific folder in SD card. I am able to get the item position but unfortunatly, I am facing a problem of displaying selected images in full screen image activity. I think there is an issue in getting image path using its position. I would appreciate your help.
This is Gridview Activity
public class print extends AppCompatActivity {
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();
public ImageAdapter(Context c) {
mContext = c;
}
void add(String path){
itemList.add(path);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
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(700, 1150));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(1, 1, 1, 1);
} else {
imageView = (ImageView) convertView;
}
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220, 220);
imageView.setImageBitmap(bm);
return imageView;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
Bitmap bm = null;
// 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;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public 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;
}
}
ImageAdapter myImageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
setContentView(R.layout.activity_print);
GridView gridView = (GridView) findViewById(R.id.gridView);
myImageAdapter = new ImageAdapter(this);
gridView.setAdapter(myImageAdapter);
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/sdfolder/";
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files){
myImageAdapter.add(file.getAbsolutePath());
}
gridView.setOnItemClickListener(myOnItemClickListener);
}
AdapterView.OnItemClickListener myOnItemClickListener
= new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// String prompt = (String)parent.getItemAtPosition(position).toString();
Intent i = new Intent(print.this, DetailsActivity.class);
Toast.makeText(print.this, "Position " + position,
Toast.LENGTH_SHORT).show();
i.putExtra("id", id);
startActivity(i);
}};
}
This is the Full Screen Activity
public class DetailsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_activity);
// Get position from intent passed from MainActivity.java
Intent i = getIntent();
int position = i.getExtras().getInt("id");
// Open the Image adapter
// Locate the ImageView in single_item_view.xml
ImageView imageView = (ImageView) findViewById(R.id.image);
// Get image and position from ImageAdapter.java and set into ImageView
imageView.setImageResource(position);
}
}
The integer you pass to ImageView.setImageResource() needs to be a valid drawable resource id. What you actually pass to the details activity is an adapter-specific item id, moreover in your case it is always 0, according to your code:
// ...
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
// ...
Solution: If you only need to show the image, you could simply pass the image URI to the details activity:
GridViewActivity.java
Intent intent = new Intent(print.this, DetailsActivity.class);
intent.putExtra("uri", myImageAdapter.itemList.get(position));
startActivity(intent);
DetailsActivity.java
// ...
setContentView(R.layout.details_activity);
// Get position from intent passed from MainActivity.java
Intent intent = getIntent();
String imageUri = intent.getExtras().getString("uri");
Bitmap image = ...; // load the full size image using the imageUri
ImageView imageView.setImageBitmap(image);
There's no need to share the list between your activities.