이미지 활동 나 SD 카드에서
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
}
내가 할 수있는 ' 내 arraylist 문자열 때문에 제대로 전달할 수 없습니다. 내가 아는 여러 방법을 시도했지만 제대로 작동하지 않습니다. 나는 인터넷에서 또한 시도했다. 그러나 일하지 않았다. 전체 이미지 활동을 통해 내 이미지를 표시 할 수 없습니다. SD 카드에서 이미지를 읽으므로 전체보기에 표시되지 않습니다. 미리 감사드립니다.
'FullImageActivity'에서'ImageAdapter'의 새로운 인스턴스를 만들고'ImageAdapter.add()'를 절대 호출하지 않기 때문에이 인스턴스의'ArrayList images'를 비 웁니다. 따라서'getCount()'는 0을 반환하고'getView()'는 결코 트리거되지 않으므로이 시점에서 비트 맵은 null입니다. 자바에서 객체 인스턴스화에 대해 조금 더 읽은 다음 나중에 Android에서 한 활동에서 다른 활동으로 데이터를 전달하는 방법에 대해 읽어 보시기 바랍니다. –
George