这是我的图库程序,我在其中显示静态文件的内容,如何修改此程序以显示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;
}
}
}
答案 0 :(得分:1)
您应首先在Manifest中设置权限:
<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));
其中file
是getImageFilesFromSD()
函数返回的ArrayList的元素。
答案 1 :(得分:0)
Environment.getExternalStoragePublicDirectory(String type)
功能可让您访问外部资源。对于最常用的标准文件夹名称,甚至还有其他内容。
不要忘记将权限放在清单上。