我想要在ViewPager中显示的Uri的ArrayList,
这是我的代码看起来的样子,我已经展示了如何将URi放入位图和drawable:
public class ImageViewActivity extends Activity {
private List<String> selectedItems;
private Uri uri1;
private Drawable drawable1;
private Bitmap bitmap1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new TouchImageAdapter());
selectedItems = new ArrayList<String>();
Intent intent = getIntent();
selectedItems = intent.getStringArrayListExtra("selectedItems");
Log.d(ImageViewActivity.class.getSimpleName(), "" + selectedItems.get(0));
uri1 = Uri.parse( selectedItems.get(0));
try {
InputStream inputStream = getContentResolver().openInputStream(uri1);
drawable1 = Drawable.createFromStream(inputStream, uri1.toString());
} catch (FileNotFoundException e) {
drawable1 = getResources().getDrawable(R.drawable.image_for_empty_url);
}
Bitmap myBitmap = BitmapFactory.decodeFile(selectedItems.get(0));
bitmap1 = myBitmap;
}
static class TouchImageAdapter extends PagerAdapter {
private static int[] images = {};
@Override
public int getCount() {
return images.length;
}
@Override
public View instantiateItem(ViewGroup container, int position) {
TouchImageView img = new TouchImageView(container.getContext());
img.setImageResource(images[position]);
container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
return img;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
但我正在查看的参考代码将其放在&#34;可绘制资源文件夹&#34;中,代码如下所示:
public class ViewPagerExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager_example);
ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new TouchImageAdapter());
}
static class TouchImageAdapter extends PagerAdapter {
private static int[] images = { R.drawable.nature_1, R.drawable.nature_2, R.drawable.nature_3, R.drawable.nature_4, R.drawable.nature_5 };
@Override
public int getCount() {
return images.length;
}
@Override
public View instantiateItem(ViewGroup container, int position) {
TouchImageView img = new TouchImageView(container.getContext());
img.setImageResource(images[position]);
container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
return img;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
我想在ImageAdapter中使用我的每个URI,我只是无法弄清楚如何将它们放入&#34; ImageAdapter。
答案 0 :(得分:0)
首先从Uri获取图像的绝对路径因为我们无法使用图像Uri将图像转换为位图,所以只需借助以下代码就可以得到绝对路径
Uri ImageURI = "your image uri goes here";
String selectedImagePath = ImageFilePath.getPath(getApplicationContext(), ImageURI);
File imageFile = new File(selectedImagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
然后在ImageView中设置图像位图
imageView.setImageBitmap (bitmap);
希望这能解决您的问题。