[英]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類。
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滾動圖像。 全屏打開時,我正在獲取圖像的路徑。 因此,只有該文件,我才可以共享路徑。 因此,當我滾動瀏覽圖像時,我無法共享圖像。 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;
}
因此,如果我能夠讀取圖像的確切路徑。 我想我會為這個問題找到解決方案。
請幫助我解決問題。
由於您的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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.