在模拟器上写入存储

Storage Write on Emulator

我正在使用 Android Studio 3.6 的模拟器开发 android 应用程序。我正在使用带有 andoird 10 的 Nexus 7 虚拟设备。

我已授予对清单文件的权限,并已请求用户授予对外部存储的读写权限。

虽然权限没问题,但我无法在外部存储上创建文件夹。我尝试编写一个后台进程。

这是 og 清单文件的摘录

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

这是我的 Main Activity 的摘录,我在其中请求用户授予权限并调用后台进程:UnitDataUpdater

public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    public static final int MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE = 1;
    // public static String SERVER_API_URL =  "http://local.xxxx.xxxxx.eu/index.php?option=com_alumincocc&view=examlist&Itemid=122&format=json&unitCode=";
    // public static LinearLayout examList;
    public JSONArray uiData;
    ListView examsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkPermissions();
        setContentView(R.layout.activity_main);
        String url = "http://xxxxx.xxxxx.eu/index.php?option=com_alumincocc&view=examlist&Itemid=122&format=json&unitCode=478050";
        String result = "";
        examsList = (ListView) findViewById(R.id.examsList);
        UnitDataUpdater process = new UnitDataUpdater(this);
        try {
            result = process.execute(url).get();

        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    ........



    public void checkPermissions() {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED
                ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super
                .onRequestPermissionsResult(requestCode,
                        permissions,
                        grantResults);

        if (requestCode == MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE) {

            // Checking whether user granted the permission or not.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // Showing the toast message
                Toast.makeText(MainActivity.this,
                        "Write Permission Granted",
                        Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(MainActivity.this,
                        "Write Permission Denied",
                        Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

}

这是我尝试写入外部存储的后台进程。 getAppDi 方法是我尝试创建新文件夹的地方。永远不会创建此文件夹,因此 class 的其余部分会引发错误。

奇怪的是,我在模拟器上看不到文档文件夹。我在 Downloads 文件夹上尝试了同样的操作,但还是一样。

public class UnitDataUpdater extends AsyncTask<String, Void, String> {
    //  private static String REMOTE_URL = "http://local.xxxxxx.xxxxx.eu/index.php?option=com_alumincocc&view=examlist&Itemid=122&format=json&unitCode=";
    private String data = "";
    public static final String REQUEST_METHOD = "GET";
    public static final int READ_TIMEOUT = 15000;
    public static final int CONNECTION_TIMEOUT = 15000;
    private String filePrefix = "i";
    private MainActivity mainActivity;

    public UnitDataUpdater(Activity mainActivity) {
        this.mainActivity = (MainActivity) mainActivity;
    }

     ........

    private String getAppDir() {
        String externalStoragePublicDirectory = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                .getAbsolutePath();


        String appDirPath = externalStoragePublicDirectory + "/XXXXUnitData";
        File appDir = new File(appDirPath);
        if (!appDir.exists()) {
            if (!appDir.mkdir()) {
                try {
                    throw new Exception("Storage Error");
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }
        return appDir.getAbsolutePath();
    }
}

感谢任何帮助

On Android Q外部存储不可读不可写。请改用 getExternalFilesDir(null)。在清单或运行时,您不需要任何读写权限。