gpt4 book ai didi

android - Android中的图库相册列表

转载 作者:搜寻专家 更新时间:2023-11-01 08:52:00 25 4
gpt4 key购买 nike

我想获取手机相册的相册列表。我试过那个代码位它不工作

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

最佳答案

试试这个:

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

关于android - Android中的图库相册列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22348432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com