最近在做一個(gè)紅包活動(dòng),要求實(shí)現(xiàn)長(zhǎng)按圖片保存到相冊(cè)以及分享的功能踏志。查詢了網(wǎng)上的一些實(shí)現(xiàn)后针余,整理成這篇文章圆雁。
主要步驟如下:
- WebView添加OnLongClickListener
- 識(shí)別長(zhǎng)按的元素類型轴咱,如果是圖片驱负,彈出AlertDialog
- 選擇保存或是分享圖片
代碼如下:
webView.setOnLongClickListener(v -> {
final WebView.HitTestResult hitTestResult = webView.getHitTestResult();
// 如果是圖片類型或者是帶有圖片鏈接的類型
if (hitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
hitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
// 彈出保存圖片的對(duì)話框
new AlertDialog.Builder(WebActivity.this)
.setItems(new String[]{"保存圖片到本地", "分享圖片"}, (dialog, which) -> {
String pic = hitTestResult.getExtra();//獲取圖片
switch (which) {
case 0:
//保存圖片到相冊(cè)
new Thread(() -> saveImage(pic)).start();
break;
case 1:
// 分享圖片,這里用RxJava處理異步
Observable.create((Observable.OnSubscribe<Bitmap>) subscriber -> subscriber.onNext(webData2bitmap(pic)))
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(bitmap -> {
try {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, getImageUri(WebActivity.this, bitmap));
startActivity(Intent.createChooser(intent, "分享圖片"));
} catch (Exception e) {
e.printStackTrace();
Util.makeText(WebActivity.this, "分享失敗");
}
}, throwable -> {
throwable.printStackTrace();
Util.makeText(WebActivity.this, "分享失敗");
});
break;
}
})
.show();
return true;
}
return false;//保持長(zhǎng)按可以復(fù)制文字
});
public void saveImage(String data) {
try {
Bitmap bitmap = webData2bitmap(data);
if (bitmap != null) {
save2Album(bitmap, new SimpleDateFormat("SXS_yyyyMMddHHmmss", Locale.getDefault()).format(new Date()) + ".jpg");
} else {
runOnUiThread(() -> Toast.makeText(WebActivity.this, "保存失敗", Toast.LENGTH_SHORT).show());
}
} catch (Exception e) {
runOnUiThread(() -> Toast.makeText(WebActivity.this, "保存失敗", Toast.LENGTH_SHORT).show());
e.printStackTrace();
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public Bitmap webData2bitmap(String data) {
byte[] imageBytes = Base64.decode(data.split(",")[1], Base64.DEFAULT);
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
}
private void save2Album(Bitmap bitmap, String fileName) {
File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DCIM), fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
runOnUiThread(() -> {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
Util.makeText(WebActivity.this, "保存成功", Toast.LENGTH_SHORT);
});
} catch (Exception e) {
runOnUiThread(() -> Util.makeText(WebActivity.this, "保存失敗", Toast.LENGTH_SHORT));
e.printStackTrace();
} finally {
try {
fos.close();
} catch (Exception ignored) {
}
}
}
網(wǎng)上別的很多文章中绘雁,hitTestResult.getExtra()
獲取到的是圖片的Url網(wǎng)址庐舟,而我們前端頁(yè)面獲取到的是base64編碼后的data
字段挪略,需要將data
還原成字節(jié)數(shù)組杠娱,再解碼成Bitmap才能保存或者分享(data
字段帶有格式信息摊求,也可直接將解碼后的字節(jié)數(shù)組寫入磁盤)刘离。
參考:
WebView實(shí)現(xiàn)長(zhǎng)按保存圖片 長(zhǎng)按識(shí)別二維碼
Android webview長(zhǎng)按圖片保存到本地
Android 的WebView長(zhǎng)按保存圖片