簡體   English   中英

如何在Android應用中獲取許可?

[英]How to get permission in android app?

我在獲取權限以從手機上的原始文件保存文件時遇到問題。 我明確地表明了這一點:

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

在我使用onclick的Java中,我具有保存文件的功能

saveTestButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

                InputStream in = null;
                FileOutputStream fout = null;
                try {
                    in = getResources().openRawResource(R.raw.testsound);
                    String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
                    String filename = "testsound.mp3";
                    fout = new FileOutputStream(new File(downloadsDirectoryPath + filename));

                    final byte data[] = new byte[1024];
                    int count;
                    while ((count = in.read(data, 0, 1024)) != -1) {
                        fout.write(data, 0, count);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fout != null) {
                        try {
                            fout.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
        }

    });

並檢查權限:

private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

當我嘗試通過單擊應用程序中的按鈕保存此文件時,出現錯誤:

W / System.err:java.io.FileNotFoundException:/storage/emulated/0/Downloadtestsound.mp3:打開失敗:EACCES(權限被拒絕)

怎么了?

我相信這是您的問題:

fout = new FileOutputStream(new File(downloadsDirectoryPath + filename));

請改用以下內容:

fout = new FileOutputStream(new File(downloadsDirectoryPath , filename));

您僅具有/storage/emulated/0/Download/testsound.mp3權限,而沒有/storage/emulated/0/Downloadtestsound.mp3權限

filenamedownloadsDirectoryPath之間放置“ /”,這就是錯誤提示的原因。如果您沒有權限,請進行測試,然后轉到設置並手動檢查那里的權限並再次啟動。

public void onClick(View v) {

        InputStream in = null;
        FileOutputStream fout = null;
        try {
            in = getResources().openRawResource(R.raw.testsound);
            String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
            String filename = "testsound.mp3";
            fout = new FileOutputStream(new File(downloadsDirectoryPath + "/"+filename));

            final byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fout != null) {
                try {
                    fout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
}

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM