int image[] = {R.drawable.d002_p001,R.drawable.d002_p002,R.drawable.d002_p003,
R.drawable.d002_p004,R.drawable.d002_p005,R.drawable.d002_p006};
showPhoto(imCurrentPhotoIndex);
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("photo_index", imCurrentPhotoIndex);
super.onSaveInstanceState(outState);
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
imCurrentPhotoIndex = savedInstanceState.getInt("photo_index");
showPhoto(imCurrentPhotoIndex);
super.onRestoreInstanceState(savedInstanceState);
}
private void showPhoto(int photoIndex) {
ImageView imageView = (ImageView) findViewById(R.id.image_view);
// imageView.setImageResource(imPhotoIds[photoIndex]);
imageView.setImageResource(imPhotoIds[photoIndex]);
}
上面的代码用于从drawable文件夹中读取和显示图像。 我想从存储卡中的文件夹中读取图像;我应该怎么做?
答案 0 :(得分:1)
您应该转到此链接http://mobile.dzone.com/news/displaying-images-sd-card您可以使用contentresolver或使用getExternalStorageDirectory获取绝对路径
答案 1 :(得分:0)
试试这段代码。
File extStore = Environment.getExternalStorageDirectory();
然后提供文件夹名称和文件名。
/audios/ringtone1.extension
答案 2 :(得分:0)
您需要先获取图像的路径。假设您的文件夹中只有图像。
ArrayList<String> thumb = new ArrayList<String>();
File[] listFile;
public void getFromSdcard()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"foldername");
if (file.isDirectory())
{
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++)
{
thumb.add(listFile[i].getAbsolutePath());
}
}
}
现在你可以使用arraylist thumb中的图像路径。
请记住在清单
中添加读取权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
将其设置为图像视图
ImageView image=(ImageView)vi.findViewById(R.id.image_view);
Bitmap b = BitmapFactory.decodeFile(thumb.get(photoIndex).toString); // get the file and decode
image.setImageBitmap(b);