@@ -56,7 +56,7 @@ public ListenableFuture<Object> delete(final Activity activity, final String pat
56
56
return Futures .immediateFailedFuture (new UnsupportedOperationException ());
57
57
}
58
58
59
- public void moveMultiple (final Activity activity , Boolean copy , String destinationDir , List <AvesImageEntry > entries , @ NonNull ImageOpCallback callback ) {
59
+ public void moveMultiple (final Activity activity , final Boolean copy , final String destinationDir , final List <AvesImageEntry > entries , @ NonNull final ImageOpCallback callback ) {
60
60
callback .onFailure (new UnsupportedOperationException ());
61
61
}
62
62
@@ -66,12 +66,11 @@ public void rename(final Activity activity, final String oldPath, final Uri oldM
66
66
return ;
67
67
}
68
68
69
- Map <String , Object > newFields = new HashMap <>();
70
69
File oldFile = new File (oldPath );
71
70
File newFile = new File (oldFile .getParent (), newFilename );
72
71
if (oldFile .equals (newFile )) {
73
72
Log .w (LOG_TAG , "new name and old name are the same, path=" + oldPath );
74
- callback .onSuccess (newFields );
73
+ callback .onSuccess (new HashMap <>() );
75
74
return ;
76
75
}
77
76
@@ -94,40 +93,7 @@ public void rename(final Activity activity, final String oldPath, final Uri oldM
94
93
}
95
94
96
95
MediaScannerConnection .scanFile (activity , new String []{oldPath }, new String []{mimeType }, null );
97
- MediaScannerConnection .scanFile (activity , new String []{newFile .getPath ()}, new String []{mimeType }, (newPath , newUri ) -> {
98
- Log .d (LOG_TAG , "onScanCompleted with newPath=" + newPath + ", newUri=" + newUri );
99
- if (newUri != null ) {
100
- // newURI is a file media URI (e.g. "content://media/12a9-8b42/file/62872")
101
- // but we need an image/video media URI (e.g. "content://media/external/images/media/62872")
102
- long contentId = ContentUris .parseId (newUri );
103
- Uri contentUri = null ;
104
- if (mimeType .startsWith (MimeTypes .IMAGE )) {
105
- contentUri = ContentUris .withAppendedId (MediaStore .Images .Media .EXTERNAL_CONTENT_URI , contentId );
106
- } else if (mimeType .startsWith (MimeTypes .VIDEO )) {
107
- contentUri = ContentUris .withAppendedId (MediaStore .Video .Media .EXTERNAL_CONTENT_URI , contentId );
108
- }
109
- if (contentUri != null ) {
110
- // we retrieve updated fields as the renamed file became a new entry in the Media Store
111
- String [] projection = {MediaStore .MediaColumns .DATA , MediaStore .MediaColumns .TITLE };
112
- try {
113
- Cursor cursor = activity .getContentResolver ().query (contentUri , projection , null , null , null );
114
- if (cursor != null ) {
115
- if (cursor .moveToNext ()) {
116
- newFields .put ("uri" , contentUri .toString ());
117
- newFields .put ("contentId" , contentId );
118
- newFields .put ("path" , cursor .getString (cursor .getColumnIndexOrThrow (MediaStore .MediaColumns .DATA )));
119
- newFields .put ("title" , cursor .getString (cursor .getColumnIndexOrThrow (MediaStore .MediaColumns .TITLE )));
120
- }
121
- cursor .close ();
122
- }
123
- } catch (Exception e ) {
124
- callback .onFailure (e );
125
- return ;
126
- }
127
- }
128
- }
129
- callback .onSuccess (newFields );
130
- });
96
+ scanNewPath (activity , newFile .getPath (), mimeType , callback );
131
97
}
132
98
133
99
public void rotate (final Activity activity , final String path , final Uri uri , final String mimeType , final boolean clockwise , final ImageOpCallback callback ) {
@@ -291,6 +257,54 @@ private void rotatePng(final Activity activity, final String path, final Uri uri
291
257
// }
292
258
}
293
259
260
+ protected void scanNewPath (final Activity activity , final String path , final String mimeType , final ImageOpCallback callback ) {
261
+ MediaScannerConnection .scanFile (activity , new String []{path }, new String []{mimeType }, (newPath , newUri ) -> {
262
+ Log .d (LOG_TAG , "scanNewPath onScanCompleted with newPath=" + newPath + ", newUri=" + newUri );
263
+
264
+ long contentId = 0 ;
265
+ Uri contentUri = null ;
266
+ if (newUri != null ) {
267
+ // newURI is possibly a file media URI (e.g. "content://media/12a9-8b42/file/62872")
268
+ // but we need an image/video media URI (e.g. "content://media/external/images/media/62872")
269
+ contentId = ContentUris .parseId (newUri );
270
+ if (mimeType .startsWith (MimeTypes .IMAGE )) {
271
+ contentUri = ContentUris .withAppendedId (MediaStore .Images .Media .EXTERNAL_CONTENT_URI , contentId );
272
+ } else if (mimeType .startsWith (MimeTypes .VIDEO )) {
273
+ contentUri = ContentUris .withAppendedId (MediaStore .Video .Media .EXTERNAL_CONTENT_URI , contentId );
274
+ }
275
+ }
276
+ if (contentUri == null ) {
277
+ callback .onFailure (new Exception ("failed to get content URI of item at path=" + path ));
278
+ return ;
279
+ }
280
+
281
+ Map <String , Object > newFields = new HashMap <>();
282
+ // we retrieve updated fields as the renamed file became a new entry in the Media Store
283
+ String [] projection = {MediaStore .MediaColumns .TITLE };
284
+ try {
285
+ Cursor cursor = activity .getContentResolver ().query (contentUri , projection , null , null , null );
286
+ if (cursor != null ) {
287
+ if (cursor .moveToNext ()) {
288
+ newFields .put ("uri" , contentUri .toString ());
289
+ newFields .put ("contentId" , contentId );
290
+ newFields .put ("path" , path );
291
+ newFields .put ("title" , cursor .getString (cursor .getColumnIndexOrThrow (MediaStore .MediaColumns .TITLE )));
292
+ }
293
+ cursor .close ();
294
+ }
295
+ } catch (Exception e ) {
296
+ callback .onFailure (e );
297
+ return ;
298
+ }
299
+
300
+ if (newFields .isEmpty ()) {
301
+ callback .onFailure (new Exception ("failed to get item details from provider at contentUri=" + contentUri ));
302
+ } else {
303
+ callback .onSuccess (newFields );
304
+ }
305
+ });
306
+ }
307
+
294
308
public interface ImageOpCallback {
295
309
void onSuccess (Map <String , Object > fields );
296
310
0 commit comments