2013-08-20 38 views
0
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]); 

    } 

上面的代碼用於讀取和顯示可繪製文件夾中的圖像。 我想要從存儲卡中的文件夾讀取圖像;我該做什麼?從存儲卡上讀取圖像ANDROID

回答

0

試試看看這個代碼。

File extStore = Environment.getExternalStorageDirectory(); 

然後給出文件夾名稱和文件名。

/audios/ringtone1.extension

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