【问题标题】:Open full Screen Image when clicked on grid view android单击网格视图android时打开全屏图像
【发布时间】: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


【解决方案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);
}

【讨论】:

  • 完美运行。非常感谢您。正如我在问题中提到的那样。我正在使用 viewPager 在 imageview 中滑动图像。我有分享按钮。当我单击共享按钮时,我可以共享用于打开全屏的图像。如果我滑动并转到下一张图片,我将无法分享该图片。可以为所有图像启用共享。
  • 请查看我的 XML 文件。
  • 如果无法分享下一张图片,会分享哪张图片?
  • 我在 gridview 中单击以打开全屏图像视图的图像。
  • 您的活动中有onOptionsItemSelected 方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多