Skip to content

Commit c15820b

Browse files
authored
fix(android): prevent errors on filesystem operations while using "content://" uris (#10461)
1 parent 41759c1 commit c15820b

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/Async.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,17 @@ private void closeOpenedStreams(Stack<Closeable> streams) throws IOException {
602602
public static class File {
603603

604604
static void updateValue(Context context, Uri uri) {
605-
ContentValues values = new ContentValues();
606-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
607-
context.getContentResolver().update(uri, values, null);
608-
} else {
609-
context.getContentResolver().update(uri, values, null, null);
605+
try {
606+
ContentValues values = new ContentValues();
607+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
608+
context.getContentResolver().update(uri, values, null);
609+
} else {
610+
context.getContentResolver().update(uri, values, null, null);
611+
}
612+
} catch (Exception exception){
613+
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
610614
}
615+
611616
}
612617

613618
public static void append(final String path, final byte[] content, final CompleteCallback callback, final Object context) {

packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/FileHelper.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.provider.DocumentsContract;
1212
import android.provider.MediaStore;
1313
import android.webkit.MimeTypeMap;
14+
import android.util.Log;
1415

1516
import androidx.annotation.Nullable;
1617
import androidx.documentfile.provider.DocumentFile;
@@ -34,6 +35,7 @@
3435
import java.util.concurrent.Executors;
3536

3637
public class FileHelper {
38+
static final String TAG = "FileHelper";
3739
private Uri uri;
3840
private final ExecutorService executor = Executors.newSingleThreadExecutor();
3941
private final Handler handler;
@@ -566,7 +568,11 @@ private void writeSyncInternal(Context context, byte[] content, boolean append)
566568
os.write(content, 0, content.length);
567569
os.flush();
568570
os.close();
569-
updateInternal(context);
571+
try {
572+
updateInternal(context);
573+
} catch (Exception exception){
574+
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
575+
}
570576
}
571577

572578
private void writeBufferSyncInternal(Context context, ByteBuffer content) throws Exception {
@@ -579,7 +585,11 @@ private void writeBufferSyncInternal(Context context, ByteBuffer content, boolea
579585
channel.write(content);
580586
os.flush();
581587
os.close();
582-
updateInternal(context);
588+
try {
589+
updateInternal(context);
590+
} catch (Exception exception){
591+
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
592+
}
583593
}
584594

585595
public void writeSync(Context context, byte[] content, @Nullable Callback callback) {
@@ -638,7 +648,11 @@ private void writeTextSyncInternal(Context context, String content, @Nullable St
638648
osw.write(content);
639649
osw.flush();
640650
osw.close();
641-
updateInternal(context);
651+
try {
652+
updateInternal(context);
653+
} catch (Exception exception){
654+
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
655+
}
642656
}
643657

644658
public void writeTextSync(Context context, String content, @Nullable String encoding, @Nullable Callback callback) {
@@ -764,14 +778,17 @@ private void renameInternal(Context context, String newName) throws Exception {
764778
}
765779

766780
}
767-
768-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
769-
context.getContentResolver().update(uri, values, null);
770-
} else {
771-
context.getContentResolver().update(uri, values, null, null);
781+
try {
782+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
783+
context.getContentResolver().update(uri, values, null);
784+
} else {
785+
context.getContentResolver().update(uri, values, null, null);
786+
}
787+
updateInternal(context, false);
788+
} catch (Exception exception){
789+
Log.e(TAG, "Failed to updateValue: " + exception.getMessage());
772790
}
773-
774-
updateInternal(context, false);
791+
775792
}
776793

777794
public void renameSync(Context context, String newName, @Nullable Callback callback) {

0 commit comments

Comments
 (0)