使用 ACTION_OPEN_DOCUMENT_TREE 選擇檔案夾后,無法將 zip 檔案保存到外部存盤。
我正在創建一個創建和操作檔案和檔案的專案,在該任務中我想將這些東西保存在外部存盤中,但我無法使用 android 開發人員檔案來做到這一點,所以請另外解釋。
我想保存這個檔案
/data/user/0/com.mobilix.docscanner/cache/abc.zip
到
content://com.android.externalstorage.documents/tree/primary:Document
我該怎么辦?
這是我的代碼,
- 用于選擇目錄以保存檔案的代碼。
saveBinding.btnInternalStorage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, PICKER_RESULT);
}
});
- 選擇目錄后執行的代碼,
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICKER_RESULT) {
if (data != null) {
Uri uri = data.getData();
String path = uri.getPath();
Log.d(TAG, "onActivityResult: uri -> " uri);
Log.d(TAG, "onActivityResult: path -> " path);
getContentResolver().takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
);
new BgExecuter().execute(new Runnable() {
@Override
public void run() {
FileManager.saveFile(PDFTools.this,selectedFile, Uri); //selectedFile is that file that is saved by default in the Temp directory
}
});
}
}else {
Toast.makeText(PDFTools.this, "dome thing happen great", Toast.LENGTH_SHORT).show();
}
}
}
方法保存檔案
public static void saveFile(Activity context, File selectedFile, Uri uri) {
context.getContentResolver().getPersistedUriPermissions();
try {
InputStream inputStream = context.getContentResolver().openInputStream(Uri.fromFile(selectedFile));
OutputStream outputStream = context.getContentResolver().openOutputStream(uri);
// get the content in bytes
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] byteArray = new byte[1024];
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
try {
byte bytes = (byte) bufferedInputStream.read(byteArray);
if (bytes >= 0) {
bufferedOutputStream.write(byteArray, 0, bytes);
bufferedOutputStream.flush();
}
inputStream.close();
outputStream.close();
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
uj5u.com熱心網友回復:
因此,要求檔案而不是目錄對用戶更好,并減少覆寫問題。
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Create a file with the requested MIME type.
intent.setType(mimeType);
// Suggest a filename
intent.putExtra(Intent.EXTRA_TITLE, "abc.zip");
// You can suggest a starting directory here see note about this later
//intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
// OK, this is deprecated and user probably has set their own request code
startActivityForResult(intent, 601);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (requestCode == 601 && resultCode == RESULT_OK) {
// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter.
// Pull that URI using resultData.getData().
Uri uri;
if (resultData != null) {
uri = resultData.getData();
new BgExecuter().execute(new Runnable() {
@Override
public void run() {
// Now that the Uri returned is for a file not a directory, your exist "saveFile" method should work
copyFileOut(PDFTools.this, selectedFile, Uri);
//selectedFile is that file that is saved by default in the Temp directory
}
});
}
}
}
private Boolean copyFileOut(Context context,File copyFile, Uri uri){
BufferedInputStream bis;
BufferedOutputStream bos = null;
// Now read the file
try{
InputStream input = new FileInputStream(copyFile);
int originalSize = input.available();
bis = new BufferedInputStream(input);
bos = new BufferedOutputStream(context.getContentResolver().openOutputStream(uri));
byte[] buf = new byte[originalSize];
//noinspection ResultOfMethodCallIgnored
bis.read(buf);
do {
bos.write(buf);
} while (bis.read(buf) != -1);
bis.close();
} catch (Exception e) {
if (BuildConfig.LOG) {
Log.e(Constants.TAG, "copyFileOut:" e);
}
// Notify User of fail
return false;
} finally {
try {
if (bos != null) {
bos.flush();
bos.close();
}
} catch (Exception ignored) {
}
}
return true;
}
初始啟動目錄的注意事項請參閱如何在用戶第一次使用我的應用程式時設定 ACTION_OPEN_DOCUMENT_TREE 啟動路徑?
另請注意,在您的“saveFile”方法中
InputStream inputStream = context.getContentResolver().openInputStream(Uri.fromFile(selectedFile));
可以替換為
InputStream inputStream = new FileInputStream(selectedFile);
正如您所說,此檔案位于您的 Apps 私有快取目錄中,因此不會有任何權限問題。
uj5u.com熱心網友回復:
字串路徑 = uri.getPath();
不不不。
你有一個很好的uri。用它。
為此樹 uri 創建一個 DocumentFile 實體。
然后使用 DocumentFile.createFile()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/427721.html
