亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在繼續之前檢查是否授予了在存儲上寫入的權限

在繼續之前檢查是否授予了在存儲上寫入的權限

慕婉清6462132 2024-01-05 11:00:24
我的應用程序出現問題。要使用選項,我需要授予外部存儲訪問權限。我在那里找到了完美的代碼:public  boolean isStoragePermissionGranted() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)            == PackageManager.PERMISSION_GRANTED) {        Log.v(TAG,"Permission is granted");        return true;    } else {        Log.v(TAG,"Permission is revoked");        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);        return false;    }}else { //permission is automatically granted on sdk<23 upon installation    Log.v(TAG,"Permission is granted");    return true;}}問題是,代碼工作正常,但我不明白如果未授予訪問權限,如何阻止我自己的代碼繼續(或等待用戶單擊“允許”,然后再繼續我的代碼)。我的代碼:isStoragePermissionGranted();        File outputFile = new File("");            InputStream is = getResources().openRawResource(((Sound) adapter.getItem(index)).getMpsound());            try {                byte[] buffer = new byte[is.available()];                is.read(buffer);                File outputDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);                outputFile = new File(outputDir, getResources().getResourceEntryName(((Sound) adapter.getItem(index)).getMpsound()) + ".mp3");                OutputStream os = new FileOutputStream(outputFile);                os.write(buffer);            } catch (IOException e) {                e.printStackTrace();            }            Intent share = new Intent(Intent.ACTION_SEND);            share.setType("audio/*");            share.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", outputFile));            share.putExtra(Intent.EXTRA_TEXT, "\"" + ((Sound) adapter.getItem(index)).getTitre_show() + "\" shared by my app !");            startActivity(Intent.createChooser(share, "Share Sound File"));            return true;目前,一切都已執行,即使未授予訪問權限,應用程序也會嘗試共享文件。所以這是行不通的。如果我返回,則會彈出請求允許訪問的窗口,等待答案。我怎樣才能做到這一點?
查看完整描述

1 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

改變這一行

isStoragePermissionGranted();

這應該可行。

if(isStoragePermissionGranted())


查看完整回答
反對 回復 2024-01-05
  • 1 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號