|
12 | 12 | import android.util.Base64;
|
13 | 13 | import android.util.Log;
|
14 | 14 |
|
| 15 | +import android.webkit.MimeTypeMap; |
15 | 16 | import android.webkit.URLUtil;
|
16 | 17 | import com.facebook.react.bridge.ActivityEventListener;
|
17 | 18 | import com.facebook.react.bridge.Arguments;
|
@@ -165,7 +166,7 @@ public void run() {
|
165 | 166 | };
|
166 | 167 | UiThreadUtil.runOnUiThread(runnable);
|
167 | 168 | }
|
168 |
| - |
| 169 | + |
169 | 170 | @ReactMethod
|
170 | 171 | public void shareText(String text,int shareScene, final Promise promise) {
|
171 | 172 | final Activity currentActivity = getCurrentActivity();
|
@@ -503,7 +504,7 @@ private String getAppName(ReactApplicationContext reactContext) {
|
503 | 504 | */
|
504 | 505 | private String processImage(String image) {
|
505 | 506 | if(URLUtil.isHttpUrl(image) || URLUtil.isHttpsUrl(image)) {
|
506 |
| - return saveBytesToFile(getBytesFromURL(image), image.toLowerCase().contains(".gif") ? "gif" : "jpg"); |
| 507 | + return saveBytesToFile(getBytesFromURL(image), getExtension(image)); |
507 | 508 | } else if (isBase64(image)) {
|
508 | 509 | return saveBitmapToFile(decodeBase64ToBitmap(image));
|
509 | 510 | } else if (URLUtil.isFileUrl(image) || image.startsWith("/") ){
|
@@ -584,12 +585,30 @@ private static byte[] getBytesFromURL(String src) {
|
584 | 585 | }
|
585 | 586 | }
|
586 | 587 |
|
| 588 | + /** |
| 589 | + * 获取链接指向文件后缀 |
| 590 | + * |
| 591 | + * @param src |
| 592 | + * @return |
| 593 | + */ |
| 594 | + public static String getExtension(String src) { |
| 595 | + String extension = null; |
| 596 | + try { |
| 597 | + URL url = new URL(src); |
| 598 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 599 | + String contentType = connection.getContentType(); |
| 600 | + extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(contentType); |
| 601 | + } catch (Exception e) { |
| 602 | + e.printStackTrace(); |
| 603 | + } |
| 604 | + return extension; |
| 605 | + } |
| 606 | + |
587 | 607 | /**
|
588 | 608 | * 将Base64解码成Bitmap
|
589 | 609 | * @param Base64String
|
590 | 610 | * @return
|
591 | 611 | */
|
592 |
| - |
593 | 612 | private Bitmap decodeBase64ToBitmap(String Base64String) {
|
594 | 613 | byte[] decode = Base64.decode(Base64String,Base64.DEFAULT);
|
595 | 614 | Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
|
|
0 commit comments