Untitled
unknown
plain_text
7 months ago
1.7 kB
1
Indexable
Never
import android.Manifest; import android.content.pm.PackageManager; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; public class YourActivity extends AppCompatActivity { private static final int STORAGE_PERMISSION_REQUEST_CODE = 1; // Call this function when you need to request storage permission private void requestStoragePermission() { // Check if the storage permission is granted if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // Permission already granted, proceed with your logic } else { // Request storage permission if not granted ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_REQUEST_CODE); } } // Override onRequestPermissionsResult to handle the result of the permission request @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == STORAGE_PERMISSION_REQUEST_CODE) { // Check if the permission is granted if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission granted, proceed with your logic } else { // Permission denied, handle accordingly (e.g., show a message or disable functionality) } } } }
Leave a Comment