2
2
package com .syanpicker ;
3
3
4
4
import android .app .Activity ;
5
- import android .content .Context ;
6
5
import android .content .Intent ;
6
+ import android .graphics .Bitmap ;
7
7
import android .graphics .BitmapFactory ;
8
+ import android .media .MediaMetadataRetriever ;
9
+ import android .os .Build ;
8
10
import android .text .TextUtils ;
9
11
import android .util .Base64 ;
10
12
30
32
import java .io .File ;
31
33
import java .io .FileInputStream ;
32
34
import java .io .FileNotFoundException ;
35
+ import java .io .FileOutputStream ;
33
36
import java .io .IOException ;
34
37
import java .io .InputStream ;
35
38
import java .text .DecimalFormat ;
36
39
import java .util .ArrayList ;
37
40
import java .util .List ;
41
+ import java .util .UUID ;
38
42
39
43
public class RNSyanImagePickerModule extends ReactContextBaseJavaModule {
40
44
@@ -330,6 +334,7 @@ private void onGetVideoResult(Intent data) {
330
334
selectList = mVideoSelectList ;
331
335
}
332
336
WritableArray videoList = new WritableNativeArray ();
337
+
333
338
for (LocalMedia media : mVideoSelectList ) {
334
339
if (TextUtils .isEmpty (media .getPath ())) {
335
340
continue ;
@@ -338,11 +343,11 @@ private void onGetVideoResult(Intent data) {
338
343
DecimalFormat df = new DecimalFormat ("#.00" );
339
344
WritableMap videoMap = new WritableNativeMap ();
340
345
341
- // File Path https://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore
342
- Context ctx = this .reactContext .getApplicationContext ();
343
- String filePath = Utils .getPathFromURI (ctx , media .getPath ());
346
+ Boolean isAndroidQ = Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ;
347
+ String filePath = isAndroidQ ? media .getAndroidQToPath () : media .getPath ();
344
348
345
- videoMap .putString ("uri" , filePath );
349
+ videoMap .putString ("uri" , "file://" + filePath );
350
+ videoMap .putString ("coverUri" , "file://" + this .getVideoCover (filePath ));
346
351
videoMap .putString ("fileName" , new File (media .getPath ()).getName ());
347
352
videoMap .putDouble ("size" , new File (media .getPath ()).length ());
348
353
videoMap .putDouble ("duration" , media .getDuration () / 1000.00 );
@@ -432,6 +437,36 @@ private String getBase64StringFromFile(String absoluteFilePath) {
432
437
return "data:image/jpeg;base64," + Base64 .encodeToString (bytes , Base64 .NO_WRAP );
433
438
}
434
439
440
+
441
+ /**
442
+ * 获取视频封面图片
443
+ * @param videoPath 视频地址
444
+ */
445
+ private String getVideoCover (String videoPath ) {
446
+ try {
447
+ MediaMetadataRetriever retriever = new MediaMetadataRetriever ();
448
+ retriever .setDataSource (videoPath );
449
+ Bitmap bitmap = retriever .getFrameAtTime ();
450
+ FileOutputStream outStream = null ;
451
+ final String uuid = "thumb-" + UUID .randomUUID ().toString ();
452
+ final String localThumb = reactContext .getExternalCacheDir ().getAbsolutePath () + "/" + uuid + ".jpg" ;
453
+ outStream = new FileOutputStream (new File (localThumb ));
454
+ bitmap .compress (Bitmap .CompressFormat .JPEG , 30 , outStream );
455
+ outStream .close ();
456
+ retriever .release ();
457
+
458
+ return localThumb ;
459
+ } catch (FileNotFoundException e ) {
460
+ e .printStackTrace ();
461
+ } catch (IOException e ) {
462
+ e .printStackTrace ();
463
+ } catch (Exception err ) {
464
+ err .printStackTrace ();
465
+ }
466
+
467
+ return null ;
468
+ }
469
+
435
470
/**
436
471
* 选择照片成功时触发
437
472
*
@@ -461,5 +496,4 @@ private void invokeError(int resultCode) {
461
496
this .mPickerPromise .reject (SY_SELECT_IMAGE_FAILED_CODE , message );
462
497
}
463
498
}
464
-
465
499
}
0 commit comments