Open full Screen Image when clicked on grid view android(单击网格视图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);
下面是被点击文件的路径.
Below i'm getting the path of the file which is clicked.
intent.putExtra("position", String.valueOf(getItem(position)));
Log.d("ImageAdapter","Intent.putExtra ");
activity.startActivity(intent);
}
这是我的 ImageDisplayActivity 类.
This is my ImageDisplayActivity class.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imageview);
获取gridview中被点击文件的路径.
Getting the path of the file which is clicked in gridview.
path = getIntent().getExtras().getString("position");
Intent i1 = getIntent();
utils = new HelperUtils(this);
imagePaths = utils.getFilePaths();
imagePaths 提供了我文件夹中的图像集合.
imagePaths gives the collection of images which inside my folder.
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 class.
公共类 FullScreenImageAdapter 扩展 PagerAdapter {
public class FullScreenImageAdapter extends 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 类运行良好.
My Touch Image class is working perfectly fine.
当我在 gridview 中单击图像中的项目时,它会全屏显示错误的图像.截至目前,我正在使用 ViewPager 滚动图像.全屏打开时,我正在获取图像的路径.因此,只有那个文件我得到了我能够共享的路径.因此,当我滚动浏览图像时,我无法共享图像.因为我没有图片的路径.
When I click an item in image in gridview, it is displaying a wrong image in full screen. As of now I'm using ViewPager to scroll image. I'm getting the path of the image when open in fullscreen. Hence only that file i'm getting the path i'm able to share. So when I scroll through the images, i'm not able to share images. Bcoz I don't have the path of the image.
这是用于启动 imageview 活动的 XML 文件
This is the XML File using for starting the imageview 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 类执行.
Whenever I swipe image from left to right my below code is getting executed from 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);
我在这里尝试获取图像的路径.但它返回的图像路径错误.
Here I'm trying to get the path of the image. But its returning a wrong path of the image.
path = String.valueOf(imagePaths.get(position));
Log.d("FullScreenImageAdapter", " path " + path);
imageView.setImageBitmap(bitmap);
((ViewPager) container).addView(view);
return view;
}
所以如果我能够读取图像的确切路径.我想我会找到解决问题的办法.
So if i'm able to read the exact path of the image. I think i'll get the solution for the problem.
请帮我解决问题.
推荐答案
由于你的 getItem
方法返回列表中不同的位置,你应该创建另一个方法来返回相应的位置,如下所示.
Since your getItem
method returns a different position on the list, you should create another method to return the corresponding position, as shown below.
@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
方法中的意图替换为
Replace your intent in your onClick
method with
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, ImageDisplayActivity.class);
intent.putExtra("id", getPosition(position));
...
这应该可以解决问题.
要解决图片分享问题,需要修改onCreateOptionsMenu
方法,添加onOptionsItemSelected
方法,如下图所示.您应该直接从 imagePaths
列表中引用图像路径.您不需要通过 Intent 发送从第一个 Activity 到下一个 Activity 的路径,因为您已经通过 Intent 发送了位置.
To solve the image sharing problem, you need to modify the onCreateOptionsMenu
method and add the onOptionsItemSelected
method as I have shown below. You should reference the image path directly from the imagePaths
list. You do not need to send the path from the first activity to the next activity via an intent because you already sent the position via the intent.
@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);
}
这篇关于单击网格视图android时打开全屏图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:单击网格视图android时打开全屏图像
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01