Created
December 3, 2020 07:30
-
-
Save edward1986/346d942965a2dd6905a585d186a267e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<Button | |
android:id="@+id/button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Request Access" | |
android:onClick="onAccessClick" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.scopeddirectoryaccess; | |
import androidx.annotation.RequiresApi; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.os.storage.StorageManager; | |
import android.os.storage.StorageVolume; | |
import android.view.View; | |
public class MainActivity extends AppCompatActivity { | |
private final int REQUEST_FOLDER_MUSIC=101; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
public void onAccessClick(View view) { | |
StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE); | |
StorageVolume storageVolume = storageManager.getPrimaryStorageVolume(); | |
Intent intent = storageVolume.createAccessIntent(Environment.DIRECTORY_MUSIC); | |
startActivityForResult(intent, REQUEST_FOLDER_MUSIC); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
switch (requestCode) { | |
case REQUEST_FOLDER_MUSIC: | |
if (resultCode == Activity.RESULT_OK) { | |
getContentResolver().takePersistableUriPermission(data.getData(), 0); | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment