Просмотр исходного кода

webview图片保存功能添加

huyuguo лет назад: 3
Родитель
Сommit
305093d510

+ 2 - 0
app/build.gradle

@ -241,6 +241,8 @@ dependencies {
241 241
    // 使用微信分享、登录、收藏、支付等功能需要的库以及文件
242 242
    implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:6.6.4'
243 243
//    implementation 'com.squareup.picasso:picasso:2.71828'
244
    // https://github.com/khoyron/Actionsheet-android
245
    implementation 'com.github.khoyron:Actionsheet-android:4'
244 246
245 247
246 248

+ 92 - 1
app/src/main/java/com/electric/chargingpile/activity/MyWebViewActivity.java

@ -12,6 +12,8 @@ import android.content.DialogInterface;
12 12
import android.content.Intent;
13 13
import android.content.SharedPreferences;
14 14
import android.graphics.Bitmap;
15
import android.graphics.Color;
16
import android.graphics.drawable.Drawable;
15 17
import android.net.Uri;
16 18
import android.net.http.SslError;
17 19
import android.os.Build;
@ -40,28 +42,34 @@ import android.widget.TextView;
40 42
import android.widget.Toast;
41 43
42 44
import androidx.annotation.NonNull;
45
import androidx.annotation.Nullable;
43 46
import androidx.core.content.FileProvider;
44 47
45 48
import com.afollestad.materialdialogs.DialogAction;
46 49
import com.afollestad.materialdialogs.MaterialDialog;
47 50
import com.blankj.utilcode.util.LogUtils;
51
import com.bumptech.glide.Glide;
52
import com.bumptech.glide.request.target.CustomTarget;
53
import com.bumptech.glide.request.transition.Transition;
48 54
import com.electric.chargingpile.BuildConfig;
49 55
import com.electric.chargingpile.R;
50 56
import com.electric.chargingpile.application.MainApplication;
51 57
import com.electric.chargingpile.data.CarOwnerCertificateBean;
52 58
import com.electric.chargingpile.data.ChargingShareBean;
53 59
import com.electric.chargingpile.data.User;
54
import com.electric.chargingpile.entity.CarSeriesEntity;
55 60
import com.electric.chargingpile.manager.ProfileManager;
56 61
import com.electric.chargingpile.util.BarColorUtil;
57 62
import com.electric.chargingpile.util.DES3;
58 63
import com.electric.chargingpile.util.ImageUitl;
59 64
import com.electric.chargingpile.util.JsonUtils;
60 65
import com.electric.chargingpile.util.LoadingDialog;
66
import com.electric.chargingpile.util.PhoneUtils;
61 67
import com.electric.chargingpile.util.ToastUtil;
62 68
import com.electric.chargingpile.util.Util;
63 69
import com.electric.chargingpile.view.AlertDialogTwo;
64 70
import com.google.gson.Gson;
71
import com.khoiron.actionsheets.ActionSheet;
72
import com.khoiron.actionsheets.callback.ActionSheetCallBack;
65 73
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
66 74
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
67 75
import com.tencent.mm.opensdk.openapi.IWXAPI;
@ -70,9 +78,12 @@ import com.umeng.analytics.MobclickAgent;
70 78
import com.zhy.http.okhttp.OkHttpUtils;
71 79
import com.zhy.http.okhttp.callback.StringCallback;
72 80
81
import org.jetbrains.annotations.NotNull;
73 82
import org.json.JSONObject;
74 83
75 84
import java.io.File;
85
import java.io.FileOutputStream;
86
import java.io.OutputStream;
76 87
import java.net.URLEncoder;
77 88
import java.util.ArrayList;
78 89
import java.util.HashMap;
@ -187,8 +198,88 @@ public class MyWebViewActivity extends Activity implements PlatformActionListene
187 198
188 199
            webView.loadUrl(mUrl);
189 200
        }
201
202
        webView.setLongClickable(true);
203
        webView.setOnLongClickListener(new View.OnLongClickListener() {
204
            @Override
205
            public boolean onLongClick(View v) {
206
                final WebView.HitTestResult result = webView.getHitTestResult(); // 获取元素内容
207
                // 判断元素类型是 图片 || 图片URL
208
                if (result.getType() == WebView.HitTestResult.IMAGE_TYPE || result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
209
                    String url = result.getExtra(); // 获取元素
210
                    Glide.with(MyWebViewActivity.this).asBitmap().load(url).into(new CustomTarget<Bitmap>() {
211
                        @Override
212
                        public void onResourceReady(@NonNull @NotNull Bitmap resource, @Nullable @org.jetbrains.annotations.Nullable Transition<? super Bitmap> transition) {
213
                            ArrayList<String> data = new ArrayList<>();
214
                            data.add("保存图像");
215
                            new ActionSheet(MyWebViewActivity.this, data)
216
                                    .hideTitle()
217
                                    .setCancelTitle("取消")
218
                                    .setColorTitle(Color.parseColor("#999999"))
219
                                    .setColorTitleCancel(Color.parseColor("#999999"))
220
                                    .setColorData(getResources().getColor(R.color.ui_green, null))
221
                                    .create(new ActionSheetCallBack() {
222
                                        @Override
223
                                        public void data(@NotNull String data, int position) {
224
                                            System.out.println(position);
225
                                            System.out.println(data);
226
                                            switch (position) {
227
                                                case 0:
228
                                                    saveImage(resource);
229
                                                    break;
230
                                            }
231
                                        }
232
                                    });
233
                        }
234
235
                        @Override
236
                        public void onLoadCleared(@Nullable @org.jetbrains.annotations.Nullable Drawable placeholder) {
237
238
                        }
239
                    });
240
                }
241
                return true;
242
            }
243
        });
244
    }
245
246
    private void saveImage(Bitmap resource) {
247
        String saveImagePath = null;
248
        String imageFileName = "d1ev_" + PhoneUtils.getGUID() + ".png";
249
        File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "d1ev");
250
        boolean success = true;
251
        if (!storageDir.exists()) {
252
            success = storageDir.mkdir();
253
        }
254
255
        if (success) {
256
            File imageFile = new File(storageDir, imageFileName);
257
            saveImagePath = imageFile.getAbsolutePath();
258
            try {
259
                OutputStream outputStream = new FileOutputStream(imageFile);
260
                resource.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
261
                outputStream.close();
262
            } catch (Exception e) {
263
                ToastUtil.showToast(MyWebViewActivity.this, "保存图像失败", Toast.LENGTH_SHORT);
264
                e.printStackTrace();
265
            }
266
267
            galleryAddPic(saveImagePath);
268
        } else {
269
            ToastUtil.showToast(MyWebViewActivity.this, "保存图像失败", Toast.LENGTH_SHORT);
270
        }
190 271
    }
191 272
273
    private void galleryAddPic(String imagePath) {
274
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
275
        File file = new File(imagePath);
276
        Uri uri = Uri.fromFile(file);
277
        intent.setData(uri);
278
        sendBroadcast(intent);
279
        ToastUtil.showToast(MyWebViewActivity.this, "图像已成功保存到相册", Toast.LENGTH_SHORT);
280
    }
281
282
192 283
    // 93 3.5.5 小程序埋点
193 284
    @JavascriptInterface
194 285
    public void showminiprogram(final String user_name, String path, int type) {

+ 2 - 2
app/src/main/java/com/electric/chargingpile/application/MainApplication.java

@ -77,9 +77,9 @@ public class MainApplication extends MultiDexApplication {
77 77
    public static String firstPoint = "";
78 78
    public static Boolean firstSsyd;
79 79
    public static String password = "";
80
    public static String url = "http://59.110.68.162";// 充电桩测试环境
80
//    public static String url = "http://59.110.68.162";// 充电桩测试环境
81 81
//    public static String pic_url = "http://59.110.68.162/zhannew/uploadfile/";
82
//    public static String url = "http://cdz.evcharge.cc";// 充电桩正式环境
82
    public static String url = "http://cdz.evcharge.cc";// 充电桩正式环境
83 83
    public static String pic_url = "http://cdz.evcharge.cc/zhannew/uploadfile/";
84 84

85 85
    //    public static String urlNew = "http://123.56.67.7:83/api/0300";// 一电测试环境