ボタンをクリックすると、電話の内部ストレージにすべてのmp3ファイルを表示しようとしています。しかし、私はボタンをクリックするとすぐに、ストレージのアクセス許可を求め、アクセス許可を与えた後、Unfortunately, app has stopped
と表示されます。実行時例外が発生しているようですが、これを解決する方法が見つかりません。誰も助けることができますか? これはボタンです:ボタンをクリックしたときに内部ストレージにリストビューとしてmp3ファイルを表示できません
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_alignStart="@+id/button3"
android:layout_below="@+id/button3"
android:layout_marginTop="22dp"
android:onClick="onClick3"
android:text="@string/button_audio" />
これはクラスである:ここでは
package com.example.dell_1.myapp3;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.File;
import java.io.IOException;
public class Bacon1 extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
private static int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
private String[] mAudioPath;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bacon1);
final Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(
new View.OnClickListener(){
public void onClick (View v){
buttonClicked(v);
abc();
}
});
}
private void buttonClicked(View view){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Snackbar.make(view, "Permission not Granted, Requesting permission.", Snackbar.LENGTH_LONG).show();
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
Snackbar.make(view, "We need permission to internal storage for displaying songs", Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(view, "Allow myapp3 to access this device's internal storage", Snackbar.LENGTH_LONG).show();
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
public void onClick2(View view) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
public void onClick4(View view) {
Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
File file = Environment.getExternalStorageDirectory();
viewIntent1.setDataAndType(Uri.fromFile(file), "video/*");
startActivity(Intent.createChooser(viewIntent1, null));
}
public void onClick5(View view) {
Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
File file = Environment.getExternalStorageDirectory();
viewIntent1.setDataAndType(Uri.fromFile(file), "zip/*");
startActivity(Intent.createChooser(viewIntent1, null));
}
public void onClick6(View view) {
Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
File file = Environment.getExternalStorageDirectory();
viewIntent1.setDataAndType(Uri.fromFile(file), "text/*");
startActivity(Intent.createChooser(viewIntent1, null));
}
public void onClick7(View view) {
Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
File file = Environment.getExternalStorageDirectory();
viewIntent1.setDataAndType(Uri.fromFile(file), "application/*");
startActivity(Intent.createChooser(viewIntent1, null));
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(Bacon1.this, "WRITE_CONTACTS granted", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(Bacon1.this, "WRITE_CONTACTS Denied", Toast.LENGTH_SHORT)
.show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
public void abc(){
mMediaPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.list);
mMusicList = getAudioList();
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
try {
playSong(mAudioPath[arg2]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getAudioList() {
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA }, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
String[] mAudioPath = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return songs;
}
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
Log.d("ringtone", "playSong :: " + path);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
}
は、Androidマニフェストファイルである。ここ
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dell_1.myapp3">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_MEDIA_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<activity
android:name=".apples"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Bacon1"
android:label="@string/title_activity_bacon1" />
<activity
android:name=".SDCard"
android:label="@string/title_activity_sdcard" />
<activity
android:name=".Songmanager"
android:label="@string/title_activity_songmanager"></activity>
</application>
</manifest>
はLogcatです:
07-02 03:40:13.655 13229-13229/com.example.dell_1.myapp3 E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
07-02 03:40:13.655 13229-13229/com.example.dell_1.myapp3 E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
07-02 03:40:13.655 13229-13229/com.example.dell_1.myapp3 E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
07-02 03:40:13.655 13229-13229/com.example.dell_1.myapp3 E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
07-02 03:40:13.700 13229-13229/com.example.dell_1.myapp3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell_1.myapp3, PID: 13229
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.dell_1.myapp3.Bacon1.abc(Bacon1.java:158)
at com.example.dell_1.myapp3.Bacon1$1.onClick(Bacon1.java:48)
at android.view.View.performClick(View.java:5609)
at android.view.View$PerformClick.run(View.java:22263)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
ねえ、あなたのlogcatの次の行をチェックしてください: com.android.providers.media.MediaProvider uri content:// media/external/audio/mediaをpid = 7359、uid = 10176から読み込むには、android.permission.READ_EXTERNAL_STORAGEまたはgrantUriPermission()が必要です。私はあなたがマニフェストXMLでマニフェストの外部アクセス許可を宣言していないと思います。 –
私はそれをしましたが、それは何も変わりません。私はもう一度プログラムを実行した後もlogcatを編集しました。 –