Этот код устарел? getExternalStoragePublicDirectory () - PullRequest
1 голос
/ 03 октября 2019

Почему этот код является моей проблемой рисования белой линии? В чем проблема? Есть ли способ заменить ее?

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);

Именно эта строка кода нарисована методом белой линии getExternalStoragePublicDirectory

1 Ответ

1 голос
/ 03 октября 2019

getExternalStoragePublicDirectory() -> устарел на уровне API 29

Для улучшения пользователяконфиденциальность, прямой доступ к общим / внешним устройствам хранения не рекомендуется. Когда приложение предназначается для Build.VERSION_CODES.Q, путь, возвращаемый этим методом, больше не доступен напрямую для приложений. Приложения могут продолжать получать доступ к содержимому, хранящемуся в общем / внешнем хранилище, путем перехода на другие альтернативы, такие как Context#getExternalFilesDir(String), MediaStore или Intent # ACTION_OPEN_DOCUMENT.

void createExternalStoragePrivateFile() {
    // Create a path where we will place our private file on external
    // storage.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");

    try {
        // Very simple code to copy a picture from the application's
        // resource into the external file.  Note that this code does
        // no error checking, and assumes the picture is small (does not
        // try to copy it in chunks).  Note that if external storage is
        // not currently mounted this will silently fail.
        InputStream is = getResources().openRawResource(R.drawable.balloons);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }
}
...