【发布时间】:2015-01-29 13:38:44
【问题描述】:
// This is my On click function which opens New activity for image view.
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, ImageDisplayActivity.class);
intent.putExtra("id", position+1);
下面是被点击文件的路径。
intent.putExtra("position", String.valueOf(getItem(position)));
Log.d("ImageAdapter","Intent.putExtra ");
activity.startActivity(intent);
}
这是我的 ImageDisplayActivity 类。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imageview);
获取gridview中被点击文件的路径。
path = getIntent().getExtras().getString("position");
Intent i1 = getIntent();
utils = new HelperUtils(this);
imagePaths = utils.getFilePaths();
imagePaths 提供了我文件夹中的图像集合。
position = i1.getExtras().getInt("id");
给出图像的位置。
pagerAdapter = new FullScreenImageAdapter(this, imagePaths);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(position);
}
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.display,menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
shareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent shareIntent = new Intent();
if(shareActionProvider != null){
imageFile = new File(path);
imageUri = Uri.fromFile(imageFile);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
if(shareActionProvider != null){
shareActionProvider.setShareIntent(shareIntent);
}
}
return true;
}
FullScreenImageAdapter 类。
公共类 FullScreenImageAdapter 扩展 PagerAdapter {
private Activity activity;
private ArrayList<String> imagePaths;
private LayoutInflater inflater;
public FullScreenImageAdapter(Activity activity,ArrayList<String> imagePaths){
this.activity = activity;
this.imagePaths = imagePaths;
}
@Override
public int getCount() {
return this.imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
public Object instantiateItem(ViewGroup container, int position){
final TouchImageView imageView;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_fullscreen_image, container,false);
imageView = (TouchImageView) view.findViewById(R.id.fullscreenimage);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePaths.get(position), options);
imageView.setImageBitmap(bitmap);
((ViewPager) container).addView(view);
return view;
}
public void destroyItem(ViewGroup container, int position, Object object){
(container).removeView((RelativeLayout) object);
}
}
我的 Touch Image 类运行良好。
当我在 gridview 中单击图像中的项目时,它会全屏显示错误的图像。截至目前,我正在使用 ViewPager 滚动图像。全屏打开时,我正在获取图像的路径。因此,只有那个文件我得到了我能够共享的路径。因此,当我滚动浏览图像时,我无法共享图像。因为我没有图片的路径。
这是用于启动 imageview 活动的 XML 文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000">
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/pager"/>
</RelativeLayout>
For Full Screen Class
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.android.example.TouchImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/fullscreenimage"
android:scaleType="fitCenter"
/>
</RelativeLayout>
每当我从左向右滑动图像时,我下面的代码就会从 FullScreemImageAdapter 类执行。
public Object instantiateItem(ViewGroup container, int position){
final TouchImageView imageView;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_fullscreen_image, container,false);
imageView = (TouchImageView) view.findViewById(R.id.fullscreenimage);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePaths.get(position), options);
我在这里尝试获取图像的路径。但它返回的图像路径错误。
path = String.valueOf(imagePaths.get(position));
Log.d("FullScreenImageAdapter", " path " + path);
imageView.setImageBitmap(bitmap);
((ViewPager) container).addView(view);
return view;
}
所以如果我能够读取图像的确切路径。我想我会找到解决问题的办法。
请帮我解决问题。
【问题讨论】:
-
如果显示错误的图像,它显示的是什么图像?
-
如果我点击 gridview 中的第一张图片。它在gridview中显示最后一张图像。例如,如果我在 gridview 中有汽车、自行车、电脑和笔记本电脑的图像。如果单击自行车而不是打开自行车的全屏图像。它会打开笔记本电脑的全屏图像。但是返回的路径是自行车的路径。只有在显示时才会显示错误的图像。
标签: android camera imageview android-camera share