如何通过单击android中的列表视图打开图像?
Posted
技术标签:
【中文标题】如何通过单击android中的列表视图打开图像?【英文标题】:How do i open an image by clicking on a listview in android? 【发布时间】:2015-10-30 08:26:35 【问题描述】:所以,事情就是这样。我想尝试一下。所以,我编写了这个程序,它在我的 GennyMotion 模拟器的 mnt/shared/Newpictures 文件夹中查找扩展名为 (.jpg) 的图像。在我的程序中,我在字符串数组适配器中捕获了扩展名为 .jpg 的文件的名称,并在文件路径字符串数组中捕获了文件路径。现在,这是我空白的部分,我有路径,名称,我可以通过单击列表来获取位置。但是当我单击列表时如何打开图像。我在网上查了一下,大部分代码都太混乱了。所以,可能有人可以建议我一个更简单的方法。这是我到目前为止所尝试的。谢谢。
package com.example.user.imageapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.io.File;
import java.util.ArrayList;
/**
* Created by user on 06-08-2015.
*/
public class Splash extends Activity
Button load;
String s;
private ListView mainList;
private String[] FilePathStrings;
ArrayList<String>filesinFolder = GetFiles("/mnt/shared/NewPictures");
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// load = (Button)findViewById(R.id.Load);
mainList = (ListView) findViewById(R.id.imagelist);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,filesinFolder );
mainList.setAdapter(adapter);
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
//what do i put here
);
public ArrayList<String> GetFiles(String DirectoryPath)
ArrayList<String> MyFiles = new ArrayList<String>();
File f = new File(DirectoryPath);
f.mkdirs();
File[] files = f.listFiles();
FilePathStrings = new String[files.length];
for (int i = 0; i < files.length; i++)
// Get the path of the image file
if(files[i].getName().contains(".jpg"))
FilePathStrings[i] = files[i].getAbsolutePath();
// Get the name image file
MyFiles.add(files[i].getName());
return MyFiles;
【问题讨论】:
【参考方案1】:实现此目的的一种简单方法是创建一个包含 imageview 的对话框,然后将 imageview 的图像设置到文件中。像这样的东西应该可以完成这项工作:
public void onItemclick(AdapterView<?> adapterView, View view, int pos, long id)
String filePath = filesInFolder.get(pos);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_img_preview, null);
builder.setView(view);
ImageView imgView = (ImageView)view.findViewById(R.id.preview_imgview);
imgView.setImageBitmap(BitmapFactory.decodeFile(filePath));
AlertDialog dialog = builder.create();
dialog.show();
【讨论】:
【参考方案2】:这里是一个简单的例子,你可以通过点击gridview查看图像,但你可以在xml文件中更改为listview。
这是gridview显示。
GridViewActivity.java
public class GridViewActivity extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(GridViewActivity.this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
// Send intent to SingleViewActivity
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
);
该类将在单页中显示图像。
SingleViewActivity.java
public class SingleViewActivity extends Activity
ImageLoader imageLoader = ImageLoader.getInstance();
private DisplayImageOptions options;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisk(true).considerExifParams(true)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.bitmapConfig(Bitmap.Config.RGB_565).build();
// Get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
//imageView.setImageResource(imageAdapter.mThumbnames[position]);
imageLoader.displayImage(imageAdapter.mThumbnames[position],imageView,options);
我使用了一些来自互联网的随机图片。列表视图中使用相同的概念来显示图像。
ImageAdapter.java
public class ImageAdapter extends BaseAdapter
private Context mContext;
private LayoutInflater layoutInflater;
ImageLoader imageLoader = ImageLoader.getInstance();
private DisplayImageOptions options;
// Constructor
public ImageAdapter(Context c)
mContext = c;
layoutInflater = LayoutInflater.from(mContext);
options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisk(true).considerExifParams(true)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.bitmapConfig(Bitmap.Config.RGB_565).build();
public int getCount()
return mThumbnames.length;
public Integer getItem(int position)
return position;
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
ViewHolder1 holder;
if (convertView == null)
convertView = layoutInflater.inflate(R.layout.grideview_item, null);
holder = new ViewHolder1();
holder.image = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
else
holder = (ViewHolder1) convertView.getTag();
// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view
// which implements ImageAware interface)
//imageLoader.displayImage("drawable://" + mThumbIds[position], holder.image);
imageLoader.displayImage(mThumbnames[position], holder.image, options);
//holder.image.setImageDrawable(mContext.getResources().getDrawable(mThumbIds[position]));
return convertView;
public static class ViewHolder1
ImageView image;
// Keep all Images in array
/* public Integer[] mThumbIds =
R.drawable.ab, R.drawable.ac,
R.drawable.ad, R.drawable.ae
;*/
public String[] mThumbnames =
"http://cdn2.ubergizmo.com/wp-content/uploads/2012/05/android_lock.jpg",
"http://cdn2.ubergizmo.com/wp-content/uploads/2012/05/android_lock.jpg",
"http://cdn2.ubergizmo.com/wp-content/uploads/2012/05/android_lock.jpg",
"http://cdn2.ubergizmo.com/wp-content/uploads/2012/05/android_lock.jpg",
"http://cdn2.ubergizmo.com/wp-content/uploads/2012/05/android_lock.jpg",
"http://cdn2.ubergizmo.com/wp-content/uploads/2012/05/android_lock.jpg",
"http://cdn2.ubergizmo.com/wp-content/uploads/2012/05/android_lock.jpg",
"http://cdn2.ubergizmo.com/wp-content/uploads/2012/05/android_lock.jpg"
;
gridview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:layout_gravity="center_horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_
android:layout_
android:layout_gravity="left"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:scaleType="fitXY" />
<TextView
android:id="@+id/textView2"
android:layout_
android:layout_
android:layout_gravity="center|bottom"
android:layout_marginBottom="5dip"
android:background="#80000000"
android:gravity="center"
android:text="images"
android:textColor="#000000"
android:textSize="20dp" />
</FrameLayout>
【讨论】:
以上是关于如何通过单击android中的列表视图打开图像?的主要内容,如果未能解决你的问题,请参考以下文章