Skip to content

Commit 71afdc6

Browse files
author
亢奋猫
committed
feat(openVideoPicker): add coverUri property for android
1 parent 01d7900 commit 71afdc6

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,25 +350,28 @@ options 可选配置:
350350
| height | number |||
351351
| type | string |||
352352
| mime | string |||
353-
| coverUri | string || |
353+
| coverUri | string || |
354354
| favorite | string |||
355355
| mediaType | string |||
356356

357357
Android 返回结果:
358358

359359
```javascript
360360
{
361-
"mime": "video/mp4",
362-
"type": "video",
363-
"height": 1080,
364-
"width": 1920,
365-
"duration": 30.22,
366-
"size": 63876724,
367-
"fileName": "VID_20200409_11492864.mp4",
368-
"uri": "file:///storage/emulated/0/DCIM/Camera/VID_20200409_11492864.mp4"
361+
mime: "video/mp4",
362+
type: "video",
363+
height: 1080,
364+
width: 1920,
365+
duration: 30.22,
366+
size: 63876724,
367+
fileName: "VID_20200409_11492864.mp4",
368+
uri: "file:///storage/emulated/0/DCIM/Camera/VID_20200409_11492864.mp4",
369+
coverUri: "file:///storage/emulated/0/Android/data/package_id/cache/thumb-c3c99b6a.jpg"
369370
}
370371
```
371372

373+
注:uri 包含协议 "file://"
374+
372375
### 删除缓存
373376
```javascript
374377
SYImagePicker.deleteCache();

android/src/main/java/com/syanpicker/GlideEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.luck.pictureselector;
1+
package com.syanpicker;
22

33
import android.content.Context;
44
import android.graphics.Bitmap;

android/src/main/java/com/syanpicker/RNSyanImagePickerModule.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
package com.syanpicker;
33

44
import android.app.Activity;
5-
import android.content.Context;
65
import android.content.Intent;
6+
import android.graphics.Bitmap;
77
import android.graphics.BitmapFactory;
8+
import android.media.MediaMetadataRetriever;
9+
import android.os.Build;
810
import android.text.TextUtils;
911
import android.util.Base64;
1012

@@ -30,11 +32,13 @@
3032
import java.io.File;
3133
import java.io.FileInputStream;
3234
import java.io.FileNotFoundException;
35+
import java.io.FileOutputStream;
3336
import java.io.IOException;
3437
import java.io.InputStream;
3538
import java.text.DecimalFormat;
3639
import java.util.ArrayList;
3740
import java.util.List;
41+
import java.util.UUID;
3842

3943
public class RNSyanImagePickerModule extends ReactContextBaseJavaModule {
4044

@@ -330,6 +334,7 @@ private void onGetVideoResult(Intent data) {
330334
selectList = mVideoSelectList;
331335
}
332336
WritableArray videoList = new WritableNativeArray();
337+
333338
for (LocalMedia media : mVideoSelectList) {
334339
if (TextUtils.isEmpty(media.getPath())) {
335340
continue;
@@ -338,11 +343,11 @@ private void onGetVideoResult(Intent data) {
338343
DecimalFormat df = new DecimalFormat("#.00");
339344
WritableMap videoMap = new WritableNativeMap();
340345

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();
344348

345-
videoMap.putString("uri", filePath);
349+
videoMap.putString("uri", "file://" + filePath);
350+
videoMap.putString("coverUri", "file://" + this.getVideoCover(filePath));
346351
videoMap.putString("fileName", new File(media.getPath()).getName());
347352
videoMap.putDouble("size", new File(media.getPath()).length());
348353
videoMap.putDouble("duration", media.getDuration() / 1000.00);
@@ -432,6 +437,36 @@ private String getBase64StringFromFile(String absoluteFilePath) {
432437
return "data:image/jpeg;base64," + Base64.encodeToString(bytes, Base64.NO_WRAP);
433438
}
434439

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+
435470
/**
436471
* 选择照片成功时触发
437472
*
@@ -461,5 +496,4 @@ private void invokeError(int resultCode) {
461496
this.mPickerPromise.reject(SY_SELECT_IMAGE_FAILED_CODE, message);
462497
}
463498
}
464-
465499
}

0 commit comments

Comments
 (0)