Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.net.Uri;
- import android.provider.DocumentsContract;
- import android.database.Cursor;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.util.regex.Pattern;
- import org.json.JSONArray;
- import org.json.JSONObject;
- // === Inputs ===
- String treeStr = "%uri_tree"; // SAF source tree
- String targetStr = "%target"; // must be /storage/... filesystem path
- String pattern = "%pattern"; // e.g. /databases/*.db
- // --- Extract subPath + file pattern ---
- String subPath = "";
- String filePattern = pattern;
- int slashIdx = pattern.lastIndexOf("/");
- if (slashIdx != -1) {
- subPath = pattern.substring(0, slashIdx); // e.g. "/databases"
- filePattern = pattern.substring(slashIdx + 1); // e.g. "*.db"
- if (subPath.startsWith("/")) subPath = subPath.substring(1);
- }
- // Convert wildcard → regex
- String regex = filePattern.replace(".", "\\.").replace("*", ".*").replace("?", ".");
- Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
- // --- Setup source tree ---
- Uri treeUri = Uri.parse(treeStr);
- String rootDocId = DocumentsContract.getTreeDocumentId(treeUri);
- Uri currentChildren = DocumentsContract.buildChildDocumentsUriUsingTree(treeUri, rootDocId);
- // Walk into subPath if provided
- if (!subPath.isEmpty()) {
- String[] parts = subPath.split("/");
- for (String part : parts) {
- Cursor c = context.getContentResolver().query(
- currentChildren,
- new String[] {
- DocumentsContract.Document.COLUMN_DOCUMENT_ID,
- DocumentsContract.Document.COLUMN_DISPLAY_NAME,
- DocumentsContract.Document.COLUMN_MIME_TYPE
- },
- null, null, null);
- boolean found = false;
- if (c != null) {
- while (c.moveToNext()) {
- String childId = c.getString(0);
- String name = c.getString(1);
- String mime = c.getString(2);
- if (name != null && name.equals(part) &&
- DocumentsContract.Document.MIME_TYPE_DIR.equals(mime)) {
- currentChildren = DocumentsContract.buildChildDocumentsUriUsingTree(treeUri, childId);
- found = true;
- break;
- }
- }
- c.close();
- }
- if (!found) {
- throw new IllegalArgumentException("Subfolder not found: " + part);
- }
- }
- }
- // --- Prepare JSON result ---
- JSONArray result = new JSONArray();
- // --- Query + match files ---
- Cursor cursor = context.getContentResolver().query(
- currentChildren,
- new String[] {
- DocumentsContract.Document.COLUMN_DOCUMENT_ID,
- DocumentsContract.Document.COLUMN_DISPLAY_NAME
- },
- null, null, null);
- if (cursor != null) {
- while (cursor.moveToNext()) {
- String childId = cursor.getString(0);
- String name = cursor.getString(1);
- if (name != null && p.matcher(name).matches()) {
- JSONObject item = new JSONObject();
- item.put("name", name);
- item.put("status", "matched");
- try {
- // Source stream
- Uri srcDocUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, childId);
- InputStream in = context.getContentResolver().openInputStream(srcDocUri);
- // Destination: filesystem file
- File destFile = new File(targetStr, name);
- if (destFile.exists()) destFile.delete(); // overwrite
- OutputStream out = new FileOutputStream(destFile);
- byte[] buffer = new byte[8192];
- int len;
- while ((len = in.read(buffer)) != -1) {
- out.write(buffer, 0, len);
- }
- in.close();
- out.close();
- item.put("copied", true);
- item.put("targetPath", destFile.getAbsolutePath());
- } catch (Exception e) {
- item.put("copied", false);
- item.put("error", e.toString());
- }
- result.put(item);
- }
- }
- cursor.close();
- }
- return result.toString();
Advertisement
Add Comment
Please, Sign In to add comment