// 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);
}
}
My Touch Image类工作正常。
当我在gridview中单击图像中的某个项目时,它将全屏显示错误的图像。截至目前,我正在使用ViewPager滚动图片。在全屏打开时,我正在获取图像的路径。因此,只有那个文件,我正在获得我能够分享的路径。因此,当我滚动图像时,我无法共享图像。 Bcoz我没有图像的路径。
这是用于启动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;
}
所以,如果我能够读取图像的确切路径。我想我会得到问题的解决方案。
请帮我解决问题。
答案 0 :(得分:1)
由于您的getItem
方法在列表中返回不同的位置,您应该创建另一种方法来返回相应的位置,如下所示。
@Override
public Object getItem(int position) {
//return this.itemList.get(itemList.size() - 1 - position);
return this.itemList.get(getPosition(position));
}
public int getPosition(int position) {
return itemList.size() - 1 - position;
}
使用
替换onClick
方法中的意图
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, ImageDisplayActivity.class);
intent.putExtra("id", getPosition(position));
...
那应该可以解决问题。
要解决图像共享问题,您需要修改onCreateOptionsMenu
方法并添加onOptionsItemSelected
方法,如下所示。您应该直接从imagePaths
列表引用图像路径。您不需要通过意图将第一个活动的路径发送到下一个活动,因为您已经通过意图发送了该位置。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display,menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
shareActionProvider = (ShareActionProvider) item.getActionProvider();
// return true;
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share:
Intent shareIntent = new Intent();
if(shareActionProvider != null){
path = imagePaths.get(pager.getCurrentItem());
imageFile = new File(path);
imageUri = Uri.fromFile(imageFile);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareActionProvider.setShareIntent(shareIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}