2013-09-23 111 views
1

我想開發一個應用程序,它在SDCard中的文件夾中創建圖像並顯示它們(現在顯示在圖庫中的「MyCreatedImages」文件夾),現在我試圖加載所有GridView佈局中來自給定文件夾的圖像。我使用下面的代碼,但是我不斷收到一個NPE,原因是我無法理解。從GridView中的特定SDCard文件夾加載圖像

而且我的GridView適配器:

private class ImageAdapter extends BaseAdapter { 
    private Context context; 

    public ImageAdapter(Context localContext) { 
     context = localContext; 
    } 

    public int getCount() { 
     return cursor.getCount(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView picturesView; 
     if (convertView == null) { 
      picturesView = new ImageView(context); 
      // Move cursor to current position 
      cursor.moveToPosition(position); 
      // Get the current value for the requested column 
      int imageID = cursor.getInt(columnIndex); 
      // Set the content of the image based on the provided URI 
      picturesView.setImageURI(Uri.withAppendedPath(
        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
        "" + imageID)); 
      picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      picturesView.setPadding(8, 8, 8, 8); 
      picturesView 
        .setLayoutParams(new GridView.LayoutParams(100, 100)); 
     } else { 
      picturesView = (ImageView) convertView; 
     } 
     return picturesView; 
    } 
} 

一旦打開這個片段,我得到以下logcat的錯誤:

09-23 16:25:06.286: E/AndroidRuntime(28497): FATAL EXCEPTION: main 
09-23 16:25:06.286: E/AndroidRuntime(28497): java.lang.NullPointerException 
09-23 16:25:06.286: E/AndroidRuntime(28497): at com.meme.hdmeme.MyMemeFragment.onCreateView(MyMemeFragment.java:51) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.os.Handler.handleCallback(Handler.java:615) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.os.Handler.dispatchMessage(Handler.java:92) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.os.Looper.loop(Looper.java:137) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at android.app.ActivityThread.main(ActivityThread.java:4921) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at java.lang.reflect.Method.invokeNative(Native Method) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at java.lang.reflect.Method.invoke(Method.java:511) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
09-23 16:25:06.286: E/AndroidRuntime(28497): at dalvik.system.NativeStart.main(Native Method) 

51號線:

columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 
+0

做一些日誌,檢查器的columnIndex等等 –

回答

4

試試這個函數從SD卡獲取圖像:

ArrayList<String> f = new ArrayList<String>(); // list of available files in path 
    File[] listFile; 

    public void getSdcardImages() 
    { 
File file= new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder"); 

    if (file.isDirectory()) 
    { 
     listFile = file.listFiles(); 


     for (int i = 0; i < listFile.length; i++) 
     { 

      f.add(listFile[i].getAbsolutePath()); 

     } 
    } 
} 

使用許可:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

然後在網格視圖通過適配器設置:

GridView imggrid = (GridView) findViewById(R.id.imgGrid); 
imgAdapter = new ImageAdapter(); 
imggrid .setAdapter(imgAdapter); 

設置適配器如下:

public class ImageAdapter extends BaseAdapter { 
private LayoutInflater mInflater; 

public ImageAdapter() { 
    mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

public int getCount() { 
    return f.size(); 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
     holder = new ViewHolder(); 
     convertView = mInflater.inflate(
       R.layout.gallery, null); 
     holder.imageview = (ImageView) convertView.findViewById(R.id.thumb); 

     convertView.setTag(holder); 
    } 
    else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 


    Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position)); 
    holder.imageview.setImageBitmap(myBitmap); 
    return convertView; 
    } 
} 

class ViewHolder { 
    ImageView imageview; 


} 
相關問題