gpt4 book ai didi

Android 图库显示来自 SD 卡的图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:19:20 26 4
gpt4 key购买 nike

这是我的图库程序,里面显示的是静态文件的内容,如何修改这个程序来显示SD卡里的照片,如果没有照片或者SD卡提示

 package DisplayViewsExample.com;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;




public class DisplayViewsExampleActivity extends Activity {

Integer[] imageIDs = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3

};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);

gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent,
View v, int position, long id)
{
/* Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();*/
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);

}
});

}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private int itemBackground;

public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}

//---returns the number of images---
public int getCount() {
return imageIDs.length;
}

//---returns the ID of an item---
public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}


}

}

最佳答案

您应该首先在 list 中设置权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

然后,编写递归地从 SD 中获取所有图像文件的函数:

final static String[] IMAGE_FORMATS = new String[] {
".JPEG", ".GIF", ".PNG", ".BMP"};

public static ArrayList<File> getImageFilesFromSD() {

File dir = Environment.getExternalStorageDirectory();

if (dir == null || !dir.exists())
return null;

ArrayList<File> images = getImageFilesFromDir(dir);
return images;

}

private static ArrayList<File> getImageFilesFromDir(File dir) {
File[] files = dir.listFiles();
ArrayList<File> result = new ArrayList<File>();
if (files != null)
for (File file : files) {
if (file.isDirectory()) {
result.addAll(getImageFilesFromDir(file));
} else {
final String fileName = file.getName();
final String fileNameLwr = fileName.toLowerCase();
// filtering images by extension
boolean contains = false;
for (int i = 0; i < IMAGE_FORMATS.length; i++) {
final String formatLwr = IMAGE_FORMATS[i].toLowerCase();
if (fileNameLwr.endsWith(formatLwr)) {
contains = true;
break;
}
}
if (contains) {
result.add(file);
}
}
}
return result;
}

之后在你的列表适配器中:

imageView.setImageURI(Uri.fromFile(file));

其中 filegetImageFilesFromSD() 函数返回的 ArrayList 的元素。

祝你好运。

关于Android 图库显示来自 SD 卡的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6829139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com