Skip to content

Commit 4404366

Browse files
authored
Merge pull request iVanPan#2 from shimo-react-native/bell
get extension from mimeType
2 parents cea2999 + 6bf8528 commit 4404366

File tree

1 file changed

+22
-3
lines changed
  • android/src/main/java/me/vanpan/rctqqsdk

1 file changed

+22
-3
lines changed

android/src/main/java/me/vanpan/rctqqsdk/QQSDK.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.util.Base64;
1313
import android.util.Log;
1414

15+
import android.webkit.MimeTypeMap;
1516
import android.webkit.URLUtil;
1617
import com.facebook.react.bridge.ActivityEventListener;
1718
import com.facebook.react.bridge.Arguments;
@@ -165,7 +166,7 @@ public void run() {
165166
};
166167
UiThreadUtil.runOnUiThread(runnable);
167168
}
168-
169+
169170
@ReactMethod
170171
public void shareText(String text,int shareScene, final Promise promise) {
171172
final Activity currentActivity = getCurrentActivity();
@@ -503,7 +504,7 @@ private String getAppName(ReactApplicationContext reactContext) {
503504
*/
504505
private String processImage(String image) {
505506
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));
507508
} else if (isBase64(image)) {
508509
return saveBitmapToFile(decodeBase64ToBitmap(image));
509510
} else if (URLUtil.isFileUrl(image) || image.startsWith("/") ){
@@ -584,12 +585,30 @@ private static byte[] getBytesFromURL(String src) {
584585
}
585586
}
586587

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+
587607
/**
588608
* 将Base64解码成Bitmap
589609
* @param Base64String
590610
* @return
591611
*/
592-
593612
private Bitmap decodeBase64ToBitmap(String Base64String) {
594613
byte[] decode = Base64.decode(Base64String,Base64.DEFAULT);
595614
Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);

0 commit comments

Comments
 (0)