【问题标题】:reading images from memory card ANDROID从存储卡 ANDROID 读取图像
【发布时间】:2013-08-20 11:18:35
【问题描述】:
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文件夹中读取和显示图像。 我想从存储卡中的文件夹中读取图像;我该怎么办?

【问题讨论】:

    标签: android


    【解决方案1】:

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

    【讨论】:

    • 感谢您的 +1 Mohammed Ahmad Osar,因为它会帮助其他人。
    【解决方案2】:

    试试这个代码。

    File extStore = Environment.getExternalStorageDirectory();
    

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

    /audios/ringtone1.extension

    【讨论】:

      【解决方案3】:

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

          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中使用图片的路径了。

      记得在manifest中添加读权限

      <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);
      

      【讨论】:

        猜你喜欢
        • 2015-04-18
        • 1970-01-01
        • 2013-04-01
        • 2012-04-21
        • 1970-01-01
        • 2011-08-06
        • 1970-01-01
        • 2021-05-25
        • 2011-12-12
        相关资源
        最近更新 更多