1. Add in Manifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. Dynamic permission acquisition
private void requestPermission(){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}else{
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
}else {
Toast.makeText(this,"Permission denied",Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestPermission();
.
.
.
}
- 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
- 29
- 30
- 31
- 32
3. Android 10 Google added a new restriction
Related instructions:
Scoped Storage
Q: How can the application temporarily exempt Q’s external storage sandbox restriction?
A: No. Therefore, we strongly recommend that the application adapt in the Q version, but if you need more time, you can consider adding application android:requestLegacyExternalStorage = “true” to the Manifest. If you want to check whether the exemption is exempt, use the Environment.isExternalStorageLegacy() function.
<application
.
.
.
android:requestLegacyExternalStorage="true"
.
.
.
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
No comments:
Post a Comment
Note: only a member of this blog may post a comment.