2012-09-27 69 views
1

如何獲取多張圖像以顯示在此封面中。我得到空指針異常和其他問題。如何能夠從SD卡加載圖像,不知道SD卡上有多少圖像?圖像數量不固定。我可以通過使用此代碼來獲得一個圖像,沒有任何問題展現出來:從SD卡中獲取多張圖像以顯示在Coverflow中

​​

它加載一個形象,一個從SD卡中指定並取得所有的CoverFlow圖像作爲一個相同的圖像,PIC03大約十幾張同樣的圖像填滿了封面流。這非常棒,但是我想將所有圖像加載到SD卡上。因此每張圖片都會填充封面流的每個部分,而不是填充所有這些圖片的同一張圖片。

完全丟失在這裏,並試圖做到這一點,並從Logcat得到空指針異常,並想知道爲什麼?有誰知道如何讓這個工作?

下面是我正在處理的代碼給你一些想法: 注意到它從SD卡上的指定地址加載一個圖像時可以工作。至少那部分工作很好,但多個圖像?

// added this code inside the onCreate method to load the cursor with images only if the IS_PRIVATE column 
// in the sqlite database is equal to the integer 1 

String[] img = { MediaStore.Images.Media._ID }; 
imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, img, 
MediaStore.Images.Media.IS_PRIVATE + "='" + 1 +"'",null, MediaStore.Images.Media._ID + ""); 
image_column_index = imagecursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); 
count = imagecursor.getCount(); 

//------------------------------------------------------------------- 
// ImageAdapter class is a nested class inside of the CoverFlowExample class 

public class ImageAdapter extends BaseAdapter { 
int mGalleryItemBackground; 
private Context mContext; 
private FileInputStream fis; 

// originally earlier version of this app loaded images from the R.drawable folder like this 

private Integer[] mImageIds = { 

    // R.drawable.pic01, 
    // R.drawable.pic02, 
    // R.drawable.pic03, 
    // R.drawable.pic04, 
    // R.drawable.pic05, 
    // R.drawable.pic06, 
    // R.drawable.pic07, 
    // R.drawable.pic08, 
    // R.drawable.pic09 
}; 

//--------------------------------------------------------------------- 
// getView() method that is in the ImageAdapter class that extends the BaseAdapter class 
// this class is a nested class inside the CoverFlowExample class that extends Activity. 
// the CoverFlowExample class is used to implement the coverflow part of the app as an Activity 

public View getView(int position, View convertView, ViewGroup parent) { 
    // to load from resources like SD card 
    ImageView i = new ImageView(mContext); 

// use for single image --> i.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/pic03.png")); 

image_column_index = imagecursor.getColumnIndexOrThrowMediaStore.Images.Media.DATA); 
imagecursor.moveToPosition(position); 

int id = imagecursor.getInt(image_column_index); 
i.setImageURIUri.withAppendedPathMediaStore   .Images.Media.EXTERNAL_CONTENT_URI, ""+ id)); 

// image from R.drawable use --> i.setImageResource(mImageIds[position]); 
i.setLayoutParams(new CoverFlow.LayoutParams(130, 130)); 
i.setScaleType(ImageView.ScaleType.MATRIX);    
return i; 

    // return mImages[position]; <-- not using this as im am not getting image from R.drawable 
} 
+2

別喊了!它會讓你的問題更難讀。他們發明了換檔鑰匙是有原因的。在所有CAPS中鍵入會使文本難以閱讀,並且不會提高您獲得答案的速度。 –

+0

我的帖子中只有5個字全部大寫,但由於您對包含所有大寫字母的詞很敏感,所以我相信其他人也是如此。預期的目的是通過強調某些要點而不是爲了鼓勵而使文本更清晰。但有些不同意,不夠公平,如果這是我在這裏的文化,我已經把這5個字改爲小寫。但是,由於通常是以這種方式寫入SD卡,所以我將「SD」字卡留在大寫字母上。無論如何謝謝你的評論。一直想改善我的溝通,並與這裏的社區合作。 – Kevik

+0

原始問題的90%是IN ALL CAPS,這是我發佈我的評論時所指的。您可以檢查編輯歷史記錄以確認。至於強調,這個網站支持** bold **和* italic *,以及'code formatting',這樣做;沒有必要爲此目的輸入CAPS。大寫的「SD」是正確的,「SDK」或「IBM」或「MS-DOS」也是如此。 –

回答

1

問題似乎String[] img = { MediaStore.Images.Media._ID } 這是包含在imagecursor僅有的一列,而你嘗試獲得DATA列,imagecursor.getColumnIndexOrThrowMediaStore.Images.Media.DATA)

相關問題