我创建了一个gridview,显示SD卡中特定文件夹中的图像。我能够获得项目位置,但不幸的是,我面临着在全屏图像活动中显示所选图像的问题。我认为使用其位置获取图像路径存在问题。我很感激你的帮助。
这是Gridview活动
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);
}};
}
这是全屏活动
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);
}
}
答案 0 :(得分:2)
您需要在第二项活动中使用ArrayList<String> itemList
,并执行以下操作:
public class DetailsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_activity);
Intent i = getIntent();
int position = i.getExtras().getInt("id");
ImageView imageView = (ImageView) findViewById(R.id.image);
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220, 220);
ImageView imageView.setImageBitmap(bm);
}
}
或者您可以将Image Uri发送到下一个活动并使用它:
public class DetailsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_activity);
Intent i = getIntent();
String imageUri = i.getExtras().getString("uri");
ImageView imageView = (ImageView) findViewById(R.id.image);
Bitmap bm = decodeSampledBitmapFromUri(imageUri, 220, 220);
ImageView imageView.setImageBitmap(bm);
}
}
答案 1 :(得分:0)
传递给ImageView.setImageResource()的整数需要是有效的可绘制资源ID。您实际传递给详细信息活动的是特定于适配器的项目ID,而且在您的情况下,根据您的代码,它始终为0:
// ...
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
// ...
解决方案:如果您只需要显示图片,只需将图片URI传递给详情活动:
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);
您无需在活动之间共享列表。