gpt4 book ai didi

java - 应用程序崩溃引发权限拒绝异常 :reading com. android.providers.media.MediaProvider

转载 作者:太空宇宙 更新时间:2023-11-04 11:20:13 25 4
gpt4 key购买 nike

我正在开发一个应用程序,需要读取和写入外部存储的权限。我已经在 list 文件以及我的java Activity 文件中写入了所有权限,但我的应用程序在启动时仍然崩溃 抛出异常

权限拒绝:读取 com.android.providers.media.MediaProvider

Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abhi.scrolling_tab">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"></uses-permission>

<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">
<activity
android:name=".MainActivity"
android:exported="true"
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=".Fragment_one"
android:label="Fragment_one"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Fragment_two"
android:label="@string/title_activity_fragment_two"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Fragment_three"
android:label="@string/title_activity_fragment_three"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

Java File

public class Fragment_two extends Fragment {
private RecyclerView recyclerView2;
private List<Model1> modelList;
private myAdapter2 adapter;
private MenuItem menu;


public Fragment_two() {
}

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

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fragment_two, container, false);
;
recyclerView2 = (RecyclerView) view.findViewById(R.id.recycle2);
int No_of_coloumns = 3;
recyclerView2.setLayoutManager(new GridLayoutManager(getContext(), No_of_coloumns));
modelList = getAllaudiofromdevice();
adapter = new myAdapter2(modelList, (ViewGroup) view);
recyclerView2.setAdapter(adapter);
return view;
}

public List<Model1> getAllaudiofromdevice() {
List<Model1> files = new ArrayList<>();
try {
ContentResolver contentResolver = getContext().getContentResolver();
String selection = MediaStore.Audio.Media.IS_MUSIC;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Audio.AudioColumns.DATA, MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.AudioColumns.ARTIST};
Cursor c = contentResolver.query(uri, null, selection, null, null);
if (c != null) {
while (c.moveToNext()) {
Model1 model1 = new Model1();
String path = c.getString(0);
String album = c.getString(1);
String artist = c.getString(2);
String name = path.substring(path.lastIndexOf('/') + 1);
model1.setName(name);
model1.setArtist(artist);
model1.setAlbum(album);
model1.setPath(path);
files.add(model1);
}
c.close();
}
} catch (Exception e) {
throw e;
}
check();
return files;
}

public void check() {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
return;
}
} else {
getAllaudiofromdevice();
}
}

@Override
public void onRequestPermissionsResult(int requestcode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestcode) {
case 123:
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
getAllaudiofromdevice();
} else {
Toast.makeText(getContext(), "you have to set permissions", Toast.LENGTH_SHORT).show();
check();
}
default:
super.onRequestPermissionsResult(requestcode, permissions, grantResults);
}

}

}

请帮助我摆脱困境。感谢期待。

最佳答案

仅在授予权限时才进行工作。您的代码包含逻辑错误:

if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { // Mistake here.
getAllaudiofromdevice();
} //...

此外,在 API 23 上,您的代码在未授予权限时将请求权限,但在已授予权限时将不执行任何操作 (*)。应该是这样的:

public void check() {
if (Build.VERSION.SDK_INT >= 23 && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Request permissions if they're not granted on API 23.
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
} else {
// Do work on API <23 and when permissions were granted on API 23+.
getAllaudiofromdevice();
}
}

(*) 根据您调用代码的方式,这确实令人困惑。

关于java - 应用程序崩溃引发权限拒绝异常 :reading com. android.providers.media.MediaProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44999674/

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