【问题标题】:Saving a file in sdcard0 and sdcard1 android在 sdcard0 和 sdcard1 android 中保存文件
【发布时间】:2016-04-04 02:17:08
【问题描述】:

我想为用户提供将视频文件存储在 sdcard0(内部存储器)或 sdcard1 中的选项。我怎样才能做到这一点。 Environment.getExternalStorageDirectory() 和 Environment.getExternalPublicStorageDirectory() 会将文件保存在 sdcard1 中。如何在 sdcard0 中保存文件?

【问题讨论】:

  • 那个解决方案对我没有帮助。他正在对路径进行硬编码。我不想硬编码路径,因为路径在不同的设备中可能会有所不同

标签: android android-sdcard


【解决方案1】:

试试这个代码

  String sdcard_path = Environment.getExternalStorageDirectory().getPath();
                    Log.d("Path ------ ", " " + sdcard_path);
                    // create a File object for the parent directory
                    File PapersDiractory = new File(sdcard_path + "/Exam Papers/");
                    // have the object build the directory structure, if needed.
                    PapersDiractory.mkdirs();
                    // create a File object for the output file
                    File outputFile = new File(PapersDiractory, "five-point-someone-chetan-bhagat_ebook.pdf");

【讨论】:

    【解决方案2】:
       ContextWrapper contextWrapper = new ContextWrapper(
                    getApplicationContext());
            File directory = contextWrapper.getDir(filepath,Context.MODE_PRIVATE);
            myInternalFile = new File(directory, filename);
    try {
                FileOutputStream fos = new FileOutputStream(myInternalFile); // save
                fos.write(myInputText.getText().toString().getBytes());
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            myInputText.setText("");
            responseText.setText("Saved to Internal Storage.(StorageFile.txt)");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多