新手面对安卓6.0以上的版本时出现一个关于文件权限检测的问题,报错为:“无法解析符号 'checkSelfPermission'”,解决办法
【【注意】:这只是笔者在遇到这个问题时的解决方法,如果对您毫无帮助,请自寻他法!!!】
面对新手:在简单做一个音乐播放程序时,如果面对安卓6.0以上的版本,就会出现一个关于文件权限检测的问题,报错为:“无法解析符号 'checkSelfPermission'”。
解决办法:将如下代码:
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
}else
{
initMediaPlayer(); // 初始化MediaPlayer
}
一定要放到onCreate函数里面去。
此外,①要保证 gradle.properties 文件的最后一行写这句代码“android.enableJetifier=true” ;
②我的这个Module叫“music_player”,点击主视图左侧project,点上方切换到project视图,在这个music_player里找到文件build.gradle ,然后在这个文件里找到如下:
defaultConfig {
applicationId "com.farsight.music_player"
minSdk 25
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
把 minSdk 数字
里的数字改成合适的数值,这里我改的25,没报错。
到这里,大功告成。
PS:具体代码如下所示,可以做参考:
MainActivity.java
package com.**此处是匿名了**.music_player;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.Manifest;
import java.io.File;
import android.content.pm.PackageManager;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
private MediaPlayer mediaPlayer = new MediaPlayer();
private Button button_play = null;
private Button button_pause = null;
private Button button_end = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_play = findViewById(R.id.button_play);
button_pause = findViewById(R.id.button_pause);
button_end = findViewById(R.id.button_end);
button_play.setOnClickListener(this);
button_pause.setOnClickListener(this);
button_end.setOnClickListener(this);
}
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
}else
{
initMediaPlayer(); // 初始化MediaPlayer
}
private void initMediaPlayer()
{
try
{
File file = new File(Environment.getExternalStorageDirectory(), "music.mp3");
mediaPlayer.setDataSource(file.getPath()); // 指定音频文件的路径
mediaPlayer.prepare(); // 让MediaPlayer进入到准备状态
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.button_play:
if(!mediaPlayer.isPlaying())
{
mediaPlayer.start();
}
break;
case R.id.button_pause:
if(!mediaPlayer.isPlaying())
{
mediaPlayer.pause();
}
break;
case R.id.button_end:
if(mediaPlayer.isPlaying())
{
mediaPlayer.reset();
initMediaPlayer();
}
break;
default:
break;
}
}
@Override
protected void onDestroy()
{
super.onDestroy();
if(mediaPlayer != null)
{
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/text_slogan"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/app_name"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.049" />
<Button
android:id="@+id/button_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:text="@string/button_play"
app:layout_constraintEnd_toEndOf="@+id/button_pause"
app:layout_constraintStart_toStartOf="@+id/button_pause"
app:layout_constraintTop_toBottomOf="@+id/text_app_name" />
<Button
android:id="@+id/button_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/button_pause"
app:layout_constraintEnd_toEndOf="@+id/button_end"
app:layout_constraintStart_toStartOf="@+id/button_end"
app:layout_constraintTop_toBottomOf="@+id/button_play" />
<Button
android:id="@+id/button_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/button_end"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_pause" />
</androidx.constraintlayout.widget.ConstraintLayout>