首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从内存卡ANDROID读取图像

从内存卡ANDROID读取图像
EN

Stack Overflow用户
提问于 2013-08-20 19:18:36
回答 3查看 148关注 0票数 0
代码语言:javascript
复制
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]);

    }

上面的代码用于读取和显示可绘制文件夹中的图像。我想从存储卡的文件夹中读取图像;我应该怎么做?

EN

回答 3

Stack Overflow用户

发布于 2013-08-20 19:32:08

您应该转到此链接http://mobile.dzone.com/news/displaying-images-sd-card您可以通过usign内容解析器或使用getExternalStorageDirectory获取绝对路径来执行此操作

票数 1
EN

Stack Overflow用户

发布于 2013-08-20 19:27:09

试试这段代码。

代码语言:javascript
复制
File extStore = Environment.getExternalStorageDirectory();

然后给出文件夹名称和文件名。

/audios/ringtone1.extension

票数 0
EN

Stack Overflow用户

发布于 2013-08-20 19:31:17

您需要首先获取图像的路径。假设您的文件夹中只有图像。

代码语言:javascript
复制
    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中的图像路径。

请记住在清单中添加读取权限

代码语言:javascript
复制
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

将其设置为图像视图

代码语言:javascript
复制
 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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18333652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档