2014-03-12 55 views
2

我想獲取手機相冊的相冊列表。 我試過碼位它不工作Android中的圖庫相冊列表

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    TextView view = new TextView(this); 
    Uri uriListURI = Uri.parse("MediaStore.Files.CONTENT_URI"); 
    Cursor cur = getContentResolver().query(uriListURI, null, null, null,null); 
    String list = ""; 
    while (cur.moveToNext()) { 
     list += cur.getString((cur.getColumnIndex("MediaStore.Files._ID")));  
    } 
    view.setText(list); 
    setContentView(view); 
}} 

我得到這些錯誤

E/AndroidRuntime(10572): FATAL EXCEPTION: main 
E/AndroidRuntime(10572): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sms/com.example.sms.MainActivity}: java.lang.NullPointerException 
E/AndroidRuntime(10572): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2313) 
E/AndroidRuntime(10572): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
E/AndroidRuntime(10572): at android.app.ActivityThread.access$600(ActivityThread.java:156) 
E/AndroidRuntime(10572): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
E/AndroidRuntime(10572): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime(10572): at android.os.Looper.loop(Looper.java:153) 
E/AndroidRuntime(10572): at android.app.ActivityThread.main(ActivityThread.java:5336) 
E/AndroidRuntime(10572): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(10572): at java.lang.reflect.Method.invoke(Method.java:511) 
E/AndroidRuntime(10572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
E/AndroidRuntime(10572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
E/AndroidRuntime(10572): at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime(10572): Caused by: java.lang.NullPointerException 
E/AndroidRuntime(10572): at com.example.sms.MainActivity.onCreate(MainActivity.java:35) 
E/AndroidRuntime(10572): at android.app.Activity.performCreate(Activity.java:5122) 
E/AndroidRuntime(10572): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081) 
E/AndroidRuntime(10572): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277) 
: E/AndroidRuntime(10572): ... 11 more 

我用同樣的方式來獲取短信和它的工作

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
TextView view = new TextView(this); 
Uri uriSMSURI = Uri.parse("content://sms/inbox"); 
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null); 
String sms = ""; 
while (cur.moveToNext()) { 
sms += "From :" + cur.getString((cur.getColumnIndex("address")));  
} 
view.setText(sms); 
setContentView(view);}} 
+0

設置一些佈局的setContentView(佈局);並添加TextView –

+0

我編輯我的帖子,所以你可以看到沒有問題的佈局 – Marya

+0

我們正確的路上。但對於'content:// sms/inbox',您在'manifest.xml'中添加了許可'SMS READ'並且 –

回答

3

試試這個:

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
TextView view = new TextView(this); 
String[] projection = new String[] {"DISTINCT " + MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME}; 
Cursor cur = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null); 
StringBuffer list = new StringBuffer(); 
while (cur.moveToNext()) { 
list.append(cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME))) + "\n"); 
} 
view.setText(list); 
setContentView(view); 
} 
+0

這正是我需要的!非常感謝@QArea – Marya