[英]Write a file in the SD card of an Android 7 device
我必須開發一個小功能,除此之外,還要在 SD 卡中寫入一個文件。 我必須將它用於供應商提供的兩個特定的 Android 平板電腦。 一台平板電腦使用Android 5,另一台使用Android 7。我正在修改的應用程序是一個系統應用程序,它沒有用戶界面。 我從Service
調用代碼,我想從FirebaseMessagingService
調用它。 我只能在 Android 7 平板電腦中寫入文件時遇到問題。
我對 Android 5 平板電腦沒有任何問題,我確定了外部存儲文件夾,我可以在其中創建文件。 但是我在 Android 7 中確實遇到了問題,我確定了外部存儲文件夾並且遇到了一個問題:權限被拒絕。
我在清單中有這個權限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
這是給我帶來問題的一段代碼:
public void myFunction()
{
String sdPath;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP)
sdPath = "/storage/extsd";
else
sdPath = "/storage/0665-3426";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (my_context.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
Log.d(TAG, "Permission is granted");
else
Log.d(TAG, "Permission is denied");
}
File folder = new File(sdPath);
if (folder.exists()) {
Log.d(TAG, sdPath + " exists, can write: " + folder.canWrite());
File file = new File(sdPath + "/new_file");
boolean fileExists = file.exists();
Log.d(TAG, file.getAbsolutePath() + " file exists: " + fileExists + ", can write: " + file.canWrite());
if (!fileExists) {
try {
file.createNewFile();
Log.d(TAG, "Can write in " + sdPath);
}
catch (Exception exception) {
Log.e(TAG, "Cannot write in " + sdPath + ": " + exception.getMessage());
}
}
}
else
Log.e(TAG, sdPath + " does not exist.");
...
}
這里是 Android 5 平板電腦的日志:
10-22 14:44:51.271 10450-10450/com.my.app D/MY_TAG: /storage/extsd exists, can write: true
10-22 14:44:51.368 10450-10450/com.my.app D/MY_TAG: /storage/extsd/new_file file exists: false, can write: false
10-22 14:44:51.479 10450-10450/com.my.app D/MY_TAG: Can write in /storage/extsd
這里是 Android 7 平板電腦中的日志:
2020-10-22 15:11:56.383 19689-19689/com.my.app D/MY_TAG: Permission is granted
2020-10-22 15:11:59.037 19689-19689/com.my.app D/MY_TAG: /storage/0665-3426 exists, can write: false
2020-10-22 15:12:07.956 19689-19689/com.my.app D/MY_TAG: /storage/0665-3426/new_file file exists: false, can write: false
2020-10-22 15:12:07.957 19689-19689/com.my.app E/MY_TAG: Cannot write in /storage/0665-3426: Permission denied
如您所見,即使授予了權限,Android 7 中的canWrite()
方法canWrite()
返回false
。您知道原因嗎? 我怎么解決這個問題?
我從堆棧溢出中閱讀了一些其他問題,但沒有找到解決方案。
我指的是Stack Overflow 線程中的一個答案。
我不知道您的情況下的目標 SDK 版本,但如果您正在構建版本 29,請嘗試將其添加到您的AndroidManifest.xml
文件中:
<manifest>
<application
<!-- other stuff -->
android:requestLegacyExternalStorage="true">
<!-- other stuff -->
</application>
</manifest>
另外,您是否在運行時正確請求權限?
自 Android Kitkat/Lollipop 以來,只能讀取可移動微型 SD 卡。
因此,您不能寫入像"/storage/0665-3426"
這樣的路徑。
可移動微型 SD 卡上只能寫入一個特定於應用程序的目錄。
要確定該文件夾的路徑,請查看由返回的第二項
getExternalFilesDirs()
我決定不使用 Android API。 由於應用程序具有提升的權限,因此我通過執行 shell 命令創建了該文件。 這是創建文件的代碼(適用於可移動 SD 卡文件夾):
public static String createFile(String filePath)
{
String returnValue = "";
try
{
Runtime runtime = Runtime.getRuntime();
String[] command = new String[]{ "su", "0", "touch", filePath};
Process p = runtime.exec(command);
p.waitFor();
java.io.BufferedReader errorIn = new java.io.BufferedReader(
new java.io.InputStreamReader(p.getErrorStream()));
String line = "";
while ((line = errorIn.readLine()) != null)
returnValue += line + "\n";
}
catch (IOException | InterruptedException e)
{
e.printStackTrace();
}
return returnValue;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.