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]);
}上面的代码用于读取和显示可绘制文件夹中的图像。我想从存储卡的文件夹中读取图像;我应该怎么做?
发布于 2013-08-20 19:32:08
您应该转到此链接http://mobile.dzone.com/news/displaying-images-sd-card您可以通过usign内容解析器或使用getExternalStorageDirectory获取绝对路径来执行此操作
发布于 2013-08-20 19:27:09
试试这段代码。
File extStore = Environment.getExternalStorageDirectory();然后给出文件夹名称和文件名。
/audios/ringtone1.extension
发布于 2013-08-20 19:31:17
您需要首先获取图像的路径。假设您的文件夹中只有图像。
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);https://stackoverflow.com/questions/18333652
复制相似问题