Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击网格视图时打开全屏图像_Android_Camera_Imageview_Android Camera_Share - Fatal编程技术网

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

Android 单击网格视图时打开全屏图像,android,camera,imageview,android-camera,share,Android,Camera,Imageview,Android Camera,Share,下面我得到了被点击的文件的路径 // 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

下面我得到了被点击的文件的路径

// 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);
这是我的ImageDisplayActivity类

            intent.putExtra("position", String.valueOf(getItem(position)));

            Log.d("ImageAdapter","Intent.putExtra ");
            activity.startActivity(intent);

        }
获取在gridview中单击的文件的路径

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imageview);
ImagePath提供了我的文件夹中的图像集合

        path = getIntent().getExtras().getString("position");

        Intent i1 = getIntent();
        utils = new HelperUtils(this);
        imagePaths = utils.getFilePaths();
给出图像的位置

        position = i1.getExtras().getInt("id");
 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);
FullScreenImageAdapter类

<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>
公共类FullScreenImageAdapter扩展了PagerAdapter{

        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;

    }
在这里,我试图得到图像的路径,但是它返回了一个错误的图像路径

        position = i1.getExtras().getInt("id");
 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);
所以如果我能读懂图像的精确路径,我想我会找到问题的解决方案


请帮助我解决问题。

由于您的
getItem
方法返回列表上的不同位置,因此您应该创建另一个方法来返回相应位置,如下所示

 path = String.valueOf(imagePaths.get(position));
    Log.d("FullScreenImageAdapter", " path " + path);
    imageView.setImageBitmap(bitmap);
    ((ViewPager) container).addView(view);
    return view;
}
onClick
方法中的意图替换为

@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;
}
这应该可以解决问题

要解决图像共享问题,您需要修改
onCreateOptions菜单
方法,并添加
onOptions ItemSelected
方法,如下所示。您应该直接从
ImagePath
列表中引用图像路径。您不需要通过I/code>发送从第一个活动到下一个活动的路径因为你已经通过意向发送了职位

@Override
 public void onClick(View v) {
     Intent intent = new Intent(activity, ImageDisplayActivity.class);

     intent.putExtra("id", getPosition(position));
 ...

如果它显示错误的图像,它会显示什么图像?如果我在gridview中单击第一个图像,它会在gridview中显示最后一个图像。例如,如果我在gridview中有汽车、自行车、计算机和笔记本电脑的图像。如果单击自行车而不是打开自行车的全屏图像,它会打开笔记本电脑的全屏图像。但是返回的路径是自行车的路径。仅当它显示时,它显示的是错误的图像。工作完美。非常感谢。正如我在问题中提到的。我正在使用viewPager在imageview中滑动图像。我有共享按钮。当我单击共享按钮时,我能够共享用于打开全屏的图像。如果我滑动并转到下一个图像,我将“我无法共享该图像。可以为所有图像启用共享。请检查我的XML文件。如果无法共享下一个图像,它将共享哪个图像?我在gridview中单击以打开全屏图像视图的图像。您的活动中是否有
onOptionsItemSelected
方法?”?