Android:从特定目录加载图像
我有一个在自定义目录中显示缩略图的图库。画廊显示良好,但我无法通过单击缩略图打开完整图像。下面是我的[无功能]单击侦听器Android:从特定目录加载图像,android,image,gallery,Android,Image,Gallery,我有一个在自定义目录中显示缩略图的图库。画廊显示良好,但我无法通过单击缩略图打开完整图像。下面是我的[无功能]单击侦听器 try { if (LoadImageFiles() == true) { GridView imgGallery = (GridView) findViewById(R.id.gallery); final ImageAdapter ia = new ImageAdapter(PersonMedia.
try {
if (LoadImageFiles() == true) {
GridView imgGallery = (GridView) findViewById(R.id.gallery);
final ImageAdapter ia = new ImageAdapter(PersonMedia.this);
imgGallery.setAdapter(ia);
// Set up a click listener
imgGallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String imgPath = paths.get(position);
Intent intent = new Intent(getApplicationContext(), ViewImage.class);
intent.putExtra("filename", imgPath);
startActivity(intent);
}
});
}
} catch (Exception ex) {
Log.e("PersonMedia.LoadPictures", ex.getMessage());
}
试试看{
如果(LoadImageFiles()==true){
GridView imgGallery=(GridView)findViewById(R.id.gallery);
最终ImageAdapter ia=新的ImageAdapter(PersonMedia.this);
imgGallery.setAdapter(ia);
//设置单击侦听器
setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
字符串imgPath=path.get(位置);
Intent Intent=新的Intent(getApplicationContext(),ViewImage.class);
intent.putExtra(“文件名”,imgPath);
星触觉(意向);
}
});
}
}捕获(例外情况除外){
Log.e(“PersonMedia.LoadPictures”,例如getMessage());
}
下面是如何填充图库的
//Declare a module level hashtable
private Hashtable<Integer, String> paths;
private boolean LoadImageFiles() {
try{
mySDCardImages = new Vector<ImageView>();
paths = new Hashtable<Integer, String>();
fileCount = 0;
sdDir = new File(imageDirectory);
sdDir.mkdir();
if (sdDir.listFiles() != null)
{
File[] sdDirFiles = sdDir.listFiles();
if (sdDirFiles.length > 0)
{
for (File singleFile : sdDirFiles)
{
Bitmap bmap = decodeFile(singleFile);
BitmapDrawable pic = new BitmapDrawable(bmap);
ImageView myImageView = new ImageView(PersonMedia.this);
myImageView.setImageDrawable(pic);
myImageView.setId(mediaCount);
paths.put(fileCount, singleFile.getAbsolutePath());
mySDCardImages.add(myImageView);
mediaCount++;
fileCount ++;
}
}
}
}
catch(Exception ex){ Log.e("LoadImageFiles", ex.getMessage()); }
return (fileCount > 0);
}
//声明模块级哈希表
私有哈希表路径;
私有布尔LoadImageFiles(){
试一试{
mySDCardImages=新向量();
路径=新哈希表();
fileCount=0;
sdDir=新文件(imageDirectory);
sdDir.mkdir();
if(sdDir.listFiles()!=null)
{
File[]sdDirFiles=sdDir.listFiles();
如果(sdDirFiles.length>0)
{
对于(单个文件:sdDirFiles)
{
位图bmap=解码文件(单文件);
BitmapDrawable pic=新的BitmapDrawable(bmap);
ImageView myImageView=新的ImageView(PersonMedia.this);
myImageView.setImageDrawable(pic);
myImageView.setId(mediaCount);
path.put(fileCount,singleFile.getAbsolutePath());
添加(myImageView);
mediaCount++;
fileCount++;
}
}
}
}
catch(异常ex){Log.e(“LoadImageFiles”,ex.getMessage());}
返回(fileCount>0);
}
您的图像是否有扩展名,如.jpg或.png?我不太确定,但看起来您正在查找的图像是personId
的字符串版本,没有任何文件扩展名
如果我错了,请纠正我
另外,请发布更多详细信息,例如您遇到的错误
更新答案如下
在向列表视图添加项目时,请使用View.setTag(Object)
(我猜是这样)。您可以调用如下内容
view.setTag("imgFile.jpg");
然后从onClickListener的内部执行以下操作:
String img = (String) getTag();
您的图像是否有扩展名,例如.jpg或.png?我不太确定,但看起来您正在查找的图像是personId
的字符串版本,没有任何文件扩展名
如果我错了,请纠正我
另外,请发布更多详细信息,例如您遇到的错误
更新答案如下
在向列表视图添加项目时,请使用View.setTag(Object)
(我猜是这样)。您可以调用如下内容
view.setTag("imgFile.jpg");
然后从onClickListener的内部执行以下操作:
String img = (String) getTag();
我能够通过使用哈希表来解决这个问题,在加载缩略图时存储图像的位置和路径。下面是两个相关的代码片段
//Where the gallery is populated and the onclick is defined
private void PopulateGallery() {
try {
if (LoadImageFiles() == true) {
GridView imgGallery = (GridView) findViewById(R.id.gallery);
imgGallery.setAdapter(new ImageAdapter(PersonMedia.this));
// Set up a click listener
imgGallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String imgPath = paths.get(position);
Intent intent = new Intent(getApplicationContext(), ViewImage.class);
intent.putExtra("filename", imgPath);
startActivity(intent);
}
});
}
} catch (Exception ex) {
Log.e("PersonMedia.LoadPictures", ex.getMessage());
}
}
//填充库并定义onclick的位置
私有void PopulateGallery(){
试一试{
如果(LoadImageFiles()==true){
GridView imgGallery=(GridView)findViewById(R.id.gallery);
setAdapter(新的ImageAdapter(PersonMedia.this));
//设置单击侦听器
setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
字符串imgPath=path.get(位置);
Intent Intent=新的Intent(getApplicationContext(),ViewImage.class);
intent.putExtra(“文件名”,imgPath);
星触觉(意向);
}
});
}
}捕获(例外情况除外){
Log.e(“PersonMedia.LoadPictures”,例如getMessage());
}
}
//图像加载的位置。您需要创建一个模块级哈希表
私有哈希表路径;
私有布尔LoadImageFiles(){
试一试{
mySDCardImages=新向量();
路径=新哈希表();
fileCount=0;
sdDir=新文件(imageDirectory);
sdDir.mkdir();
if(sdDir.listFiles()!=null)
{
File[]sdDirFiles=sdDir.listFiles();
如果(sdDirFiles.length>0)
{
对于(单个文件:sdDirFiles)
{
位图bmap=解码文件(单文件);
BitmapDrawable pic=新的BitmapDrawable(bmap);
ImageView myImageView=新的ImageView(PersonMedia.this);
myImageView.setImageDrawable(pic);
myImageView.setId(mediaCount);
path.put(fileCount,singleFile.getAbsolutePath());
添加(myImageView);
mediaCount++;
fileCount++;
}
}
}
}
catch(异常ex){Log.e(“LoadImageFiles”,ex.getMessage());}
返回(fileCount>0);
}
加载缩略图时,我可以使用哈希表存储图像的位置和路径来解决这个问题。下面是两个相关的代码片段
//Where the gallery is populated and the onclick is defined
private void PopulateGallery() {
try {
if (LoadImageFiles() == true) {
GridView imgGallery = (GridView) findViewById(R.id.gallery);
imgGallery.setAdapter(new ImageAdapter(PersonMedia.this));
// Set up a click listener
imgGallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String imgPath = paths.get(position);
Intent intent = new Intent(getApplicationContext(), ViewImage.class);
intent.putExtra("filename", imgPath);
startActivity(intent);
}
});
}
} catch (Exception ex) {
Log.e("PersonMedia.LoadPictures", ex.getMessage());
}
}
//填充库并定义onclick的位置
私有void PopulateGallery(){