gpt4 book ai didi

Android:我怎样才能访问音频文件?

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

在我的应用程序中,我使用此方法访问音频文件:

public Cursor pobierzPlikiAudio(){
String[] STAR = {"*"};
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Uri uri= Uri.parse("content://pl/aplikacja/app/provider/MediaStore/Audio");
return getContentResolver().query(uri, STAR, selection, null, null);
}

但是在我的 LogCat 中我得到一个 nullPointerException。怎么了?

Logcat 错误:

10-29 07:04:17.729: E/AndroidRuntime(24354): FATAL EXCEPTION: main
10-29 07:04:17.729: E/AndroidRuntime(24354): java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.aplikacja.app/pl.aplikacja.app.MainActivity}: java.lang.NullPointerException
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.os.Looper.loop(Looper.java:137)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.main(ActivityThread.java:4441)
10-29 07:04:17.729: E/AndroidRuntime(24354): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 07:04:17.729: E/AndroidRuntime(24354): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 07:04:17.729: E/AndroidRuntime(24354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-29 07:04:17.729: E/AndroidRuntime(24354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-29 07:04:17.729: E/AndroidRuntime(24354): at dalvik.system.NativeStart.main(Native Method)
10-29 07:04:17.729: E/AndroidRuntime(24354): Caused by: java.lang.NullPointerException
10-29 07:04:17.729: E/AndroidRuntime(24354): at pl.aplikacja.app.MainActivity.onCreate(MainActivity.java:18)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.Activity.performCreate(Activity.java:4465)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-29 07:04:17.729: E/AndroidRuntime(24354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
10-29 07:04:17.729: E/AndroidRuntime(24354): ... 11 more

这是 MainActivity.java

package pl.aplikacja.app;

import android.os.Bundle;
import android.database.Cursor;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.provider.MediaStore;

public class MainActivity extends Activity {

private Dostawca dostawca;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Cursor tablicaPlikow = dostawca.pobierzPlikiAudio();

if (tablicaPlikow.moveToFirst()) {

String title;
String artist;
String album;
int titleColumn = tablicaPlikow.getColumnIndex(MediaStore.EXTRA_MEDIA_TITLE);
int artistColumn = tablicaPlikow.getColumnIndex(MediaStore.EXTRA_MEDIA_ARTIST);
int albumColumn =tablicaPlikow.getColumnIndex(MediaStore.EXTRA_MEDIA_ALBUM);


// Get the field values
title = tablicaPlikow.getString(titleColumn);
artist = tablicaPlikow.getString(artistColumn);
album = tablicaPlikow.getString(albumColumn);

TextView widok = (TextView) findViewById(R.id.tekst);
widok.setText(""+title+" "+ artist + " " + album);
}

setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

最佳答案

我认为您输入了错误的 uri。在这个 Uri 上没有媒体文件,这就是你得到空指针异常的原因。请点击以下链接
GetMedia Information

关于Android:我怎样才能访问音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13116708/

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