public class ImageAdapter extends BaseAdapter {
private Context context;
Bitmap bm;
ArrayList<String>images = new ArrayList<String>();
// Integer[] images;
void add(String path) {
images.add(path);
}
public ImageAdapter(Context c) {
context = c;
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object getItem(int position) {
return images;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
if (convertView == null) {
// imageView.setImageResource(images[position]);
// imageView.setImageBitmap(images);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setLayoutParams(new GridView.LayoutParams(240, 240));
return imageView;
} else {
imageView = (ImageView) convertView;
}
bm = decodeSampledBitmapFromUri(images.get(position), 220, 220);
imageView.setImageBitmap(bm);
return imageView;
}
private 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;
}
}
主要活動
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i= new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("id", position);
startActivity(i);
}
});
FullImageActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image);
Intent i=getIntent();
int position= i.getExtras().getInt("id");
ImageAdapter imageAdapter= new ImageAdapter(this);
ImageView imageView=(ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(imageAdapter.bm); \\here I have to show my images that are read from Storage
}
我可以」能夠正確傳遞它,因爲我的數組列表是字符串。我嘗試了很多我知道但卻無法正常工作的方式。我也嘗試從互聯網也沒有工作。 我無法通過全圖活動顯示我的圖像。我從SD卡上讀取圖像,因此無法全屏顯示。在此先感謝
在你的'FullImageActivity'創建'ImageAdapter'和'ArrayList的 images'這種情況下的一個新的實例是空的,因爲你永遠不調用'ImageAdapter.add()'。因此,'getCount()'返回0,'getView()'從不觸發,所以你的位圖在這一點上是空的。我建議你閱讀更多關於Java中的對象實例化的內容,稍後介紹如何在Android中將數據從一個Activity傳遞到另一個Activity。 –
George