单击网格视图时打开全屏图像 android

Open full Screen Image when clicked on grid view android

// 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 class。

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;

    }

全屏图像适配器class。

public class 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 class 工作正常。

当我在 gridview 中单击图像中的项目时,全屏显示错误的图像。截至目前,我正在使用 ViewPager 滚动图像。全屏打开时,我正在获取图像的路径。因此只有那个文件我得到了我能够共享的路径。因此,当我滚动浏览图像时,我无法共享图像。因为我没有图片的路径。

这是用于启动图像视图的 XML 文件 activity

<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 class.

执行
 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;
}

因此,如果我能够读取图像的确切路径。我想我会找到问题的解决方案。

请帮我解决问题。

由于您的 getItem 方法在列表中 return 的位置不同,您应该创建另一个方法到 return 相应的位置,如下所示。

@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 列表中引用图像路径。您不需要通过意图发送从第一个 activity 到下一个 activity 的路径,因为您已经通过意图发送了位置。

@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);
}