我想获得手机画廊的专辑列表。 我尝试了那段代码,它没有工作
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 :(得分: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);
}