新手面对安卓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>
新手面对安卓6.0以上的版本时出现一个关于文件权限检测的问题,报错为:“无法解析符号 'checkSelfPermission'”,解决办法的更多相关文章
- tensorflow-gpu2.1.0报错 so returning NUMA node zero解决办法
>>> print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))2020-06-06 10:14:08.92 ...
- 关于win10 链接安卓设备报错winusb.sys未经签名的解决办法
很简单,各位,我找了一个签过名的winusb.sys替换原来的文件即可. 操作系统win10 64位专业版(更新到最新版本了) 网盘地址 安装好以后,就没有那个惊叹号咯!
- Vue3.0报错error: Unexpected console statement (no-console) 解决办法
写项目过程中用ESLint遵守代码规范很有必要,但是对于一些规范也很是无语,比如:‘Unexpected console statement (no-console)’,连console都不能用,这就 ...
- 彻底解决安卓7.0及以上版本抓包https失败
目录 现象 原因 解决办法 webview抓包失败 警告 现象 android7.0以上的手机https抓包失败(安装了https证书也不行) 原因 android7.0+的版本新增了证书验证(系统证 ...
- (转载)安卓6.0之前的系统 判断app是否有录音权限
卓6.0之前的系统 判断app是否有录音权限 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
- NAnt0.92版本首次在windows 8.1的机子上运行报错的问题解决
在官网上下载的0.92版本,各方面都配置好之后,用命令行运行,却提示报错,如下: 具体的错误提示文字是这样的: 获取ConfigurationFileLocation异常. System.Securi ...
- 使用jave1.0.2将amr文件转成其他格式报错解决方案
背景:最近需要将微信公众号里面用户发的语音文件转成其他格式的语音文件 介绍:在刚开始使用jave1.0.2 没有几行代码就可以实现,但是发现在转换的过程会报错,但是最后文件也转成功了,此时是在wind ...
- 安卓6.0之前的系统 判断app是否有录音权限
public static synchronized boolean isVoicePermission() { AudioRecord record = null; try { record = n ...
- Git版本库创建(包含文件权限设置 Linux环境下)
确保git服务已安装成功,如果没有安装git服务查看:Git源码安装 Linux指定安装目录 1.创建git用户,并设置密码.并禁止git用户通过shell登录服务器(注意如果需要安装gitolite ...
- Spring-boot2.0.1.BUILD-SNAPSHOT整合Elasticsearch报failed to load elasticsearch nodes错误解决办法
spring-boot整合es的application.properties的默认配置为: spring.data.elasticsearch.cluster-nodes=localhost:9200 ...
随机推荐
- Go优雅的错误处理: 支持错误堆栈, 错误码, 错误链的工具库
地址: https://github.com/morrisxyang/errors 如果觉得有用欢迎 Star 和 PR, 有问题请直接提issue errors 简单的支持错误堆栈, 错误码, 错误 ...
- 4.10 x64dbg 反汇编功能的封装
LyScript 插件提供的反汇编系列函数虽然能够实现基本的反汇编功能,但在实际使用中,可能会遇到一些更为复杂的需求,此时就需要根据自身需要进行二次开发,以实现更加高级的功能.本章将继续深入探索反汇编 ...
- C语言基础-基础指针
文章目录 指针 前言 1.什么是指针 2.指针的使用 (1)指针的定义 (2)指针的赋值 (3)指针类型 (4)如何使用指针 3.野指针 (1)导致野指针的原因 ① 未初始化指针 ②指针越界访问 ③指 ...
- 与AI对话 -- 20230215 -- linux 启动参数与控制台
linux 启动参数 console=ttyS0,115200n8 console=tty0 说明 console=ttyS0,115200n8:指定系统使用 ttyS0(ttyS1.ttyS2 以此 ...
- WPF 中WebBrowser 控件的“允许阻止的内容”修复(引用本地的html页)
解决方法:(个人理解:导致原因就是iE安全机制的问题吧).在你的HTML里面第一行加: <!-- saved from url=(0014)about:internet -->具体原因可以 ...
- Centos7制作本地yum仓库,共享给局域网其他设备
环境准备: 准备好安装好Centos7的虚机A(服务端)和虚机B(客户端) 配置两台虚机网络使其互通,关闭selinux和firewalld等限制 下载完整的ISO镜像(CentOS-7-x86_64 ...
- vue: 在页面中单独引入elment-ui
引入资源 首先引入Vue,之后引入element-ui. 引入组件 返回数据 全部代码 <!DOCTYPE html> <html lang="en"> & ...
- CF1601 题解
偶然看这一场的题目,忽然很有感觉,于是写了一下 A 题面 考虑每一位可以单独分开考虑 考虑单独的一位,每次要选 \(m\) 个位置,可能产生贡献的位置就是这位为 1 的数,设数量为 \(x\),则 \ ...
- cdn 引入的资源需要通过 externals 排除打包哦~
cdn 指的是通过相互连接的网络系统,使用最靠近用户的服务器将音乐.图片等资源以高效率和低成本的方式将内容传递给用户. 在 webpack 中,我们可能会将引入的第三方资源会编译成单独的文件,作为静态 ...
- dimp V8:[WARNING]login fail, check your username and password, and check the server status
在进行某个项目的性能测试时,我们选择了达梦8作为使用的数据库.因前期的网络安全问题和考虑到节省成本,我们首先在公司本地服务器上搭建了相应的环境,并生成了用于压力测试的业务数据. 然而,在将数据库迁移到 ...