|
|
|
1321
|
private boolean isPermissionOK() {
|
|
|
1322
|
return EasyPermissions.hasPermissions(this,
|
|
|
1323
|
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
|
|
1324
|
Manifest.permission.READ_EXTERNAL_STORAGE,
|
|
|
1325
|
Manifest.permission.CAMERA
|
|
|
1326
|
);
|
|
|
1327
|
}
|
|
|
1328
|
|
|
|
1329
|
private void openImageChooserActivity() {
|
|
|
1330
|
new MaterialDialog.Builder(this).items(R.array.photo).positiveText("取消")
|
|
|
1331
|
.onPositive(new MaterialDialog.SingleButtonCallback() {
|
|
|
1332
|
@Override
|
|
|
1333
|
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
|
|
|
1334
|
if (mOpenFileWebChromeClient.mFilePathCallbacks != null) {
|
|
|
1335
|
mOpenFileWebChromeClient.mFilePathCallbacks.onReceiveValue(null);
|
|
|
1336
|
mOpenFileWebChromeClient.mFilePathCallbacks = null;
|
|
|
1337
|
}
|
|
|
1338
|
|
|
|
1339
|
if (mOpenFileWebChromeClient.mFilePathCallback != null) {
|
|
|
1340
|
mOpenFileWebChromeClient.mFilePathCallback.onReceiveValue(null);
|
|
|
1341
|
mOpenFileWebChromeClient.mFilePathCallback = null;
|
|
|
1342
|
}
|
|
|
1343
|
dialog.dismiss();
|
|
|
1344
|
}
|
|
|
1345
|
}).cancelable(false)
|
|
|
1346
|
.canceledOnTouchOutside(false).itemsCallback(new MaterialDialog.ListCallback() {
|
|
|
1347
|
@Override
|
|
|
1348
|
public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
|
|
|
1349
|
if (position == 0){
|
|
|
1350
|
takeCamera();
|
|
|
1351
|
} else if (position == 1) {
|
|
|
1352
|
takePhoto();
|
|
|
1353
|
}
|
|
|
1354
|
}
|
|
|
1355
|
}).show();
|
|
|
1356
|
}
|
|
|
1357
|
|
|
|
1358
|
// 选择图片
|
|
|
1359
|
private void takePhoto() {
|
|
|
1360
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
1361
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
1362
|
intent.setType("image/*");
|
|
|
1363
|
startActivityForResult(Intent.createChooser(intent, "Image Choose"), FILE_CHOOSER_RESULT_CODE);
|
|
|
1364
|
}
|
|
|
1365
|
|
|
|
1366
|
// 拍照
|
|
|
1367
|
private void takeCamera() {
|
|
|
1368
|
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
1369
|
if (ImageUitl.hasSdcard()) {
|
|
|
1370
|
String fileName = "android" + System.currentTimeMillis() / 1000 + ".jpg";
|
|
|
1371
|
cameraFilePath = ImageUitl.getPath(Environment.getExternalStorageDirectory() + "/" + "cdz") + "/" + fileName;
|
|
|
1372
|
File imageFile = ImageUitl.getFile(cameraFilePath);
|
|
|
1373
|
Uri uri = parseUri(imageFile);
|
|
|
1374
|
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
|
|
|
1375
|
try{
|
|
|
1376
|
startActivityForResult(intent, FILE_CAMERA_RESULT_CODE);
|
|
|
1377
|
} catch (Exception e) {
|
|
|
1378
|
e.printStackTrace();
|
|
|
1379
|
}
|
|
|
1380
|
|
|
|
1381
|
}
|
|
|
1382
|
}
|
|
|
1383
|
|
|
|
1384
|
private Uri parseUri(File cameraFile) {
|
|
|
1385
|
Uri imageUri;
|
|
|
1386
|
String authority = getPackageName() + ".provider";
|
|
|
1387
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
1388
|
//通过FileProvider创建一个content类型的Uri
|
|
|
1389
|
imageUri = FileProvider.getUriForFile(getApplicationContext(), authority, cameraFile);
|
|
|
1390
|
} else {
|
|
|
1391
|
imageUri = Uri.fromFile(cameraFile);
|
|
|
1392
|
}
|
|
|
1393
|
return imageUri;
|
|
|
1394
|
}
|
|
|
1395
|
|
|
1299
|
1396
|
public class MyWebViewClient extends WebViewClient {
|
|
1300
|
1397
|
@Override
|
|
1301
|
1398
|
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
|
|
@ -1346,38 +1443,68 @@ public class MyWebViewActivity extends Activity implements PlatformActionListene
|
|
1346
|
1443
|
}
|
|
1347
|
1444
|
|
|
1348
|
1445
|
@Override
|
|
1349
|
|
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
1350
|
|
if (requestCode == OpenFileWebChromeClient.REQUEST_FILE_PICKER) {
|
|
|
1446
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
1447
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
1448
|
if (null == mOpenFileWebChromeClient.mFilePathCallback && null == mOpenFileWebChromeClient.mFilePathCallbacks) return;
|
|
|
1449
|
if (resultCode != RESULT_OK) {
|
|
|
1450
|
if (mOpenFileWebChromeClient.mFilePathCallbacks != null) {
|
|
|
1451
|
mOpenFileWebChromeClient.mFilePathCallbacks.onReceiveValue(null);
|
|
|
1452
|
mOpenFileWebChromeClient.mFilePathCallbacks = null;
|
|
|
1453
|
}
|
|
|
1454
|
|
|
1351
|
1455
|
if (mOpenFileWebChromeClient.mFilePathCallback != null) {
|
|
1352
|
|
Uri result = intent == null || resultCode != Activity.RESULT_OK ? null
|
|
1353
|
|
: intent.getData();
|
|
1354
|
|
if (result != null) {
|
|
1355
|
|
String path = MediaUtility.getPath(getApplicationContext(),
|
|
1356
|
|
result);
|
|
1357
|
|
Uri uri = Uri.fromFile(new File(path));
|
|
1358
|
|
mOpenFileWebChromeClient.mFilePathCallback
|
|
1359
|
|
.onReceiveValue(uri);
|
|
1360
|
|
} else {
|
|
1361
|
|
mOpenFileWebChromeClient.mFilePathCallback
|
|
1362
|
|
.onReceiveValue(null);
|
|
1363
|
|
}
|
|
|
1456
|
mOpenFileWebChromeClient.mFilePathCallback.onReceiveValue(null);
|
|
|
1457
|
mOpenFileWebChromeClient.mFilePathCallback = null;
|
|
1364
|
1458
|
}
|
|
|
1459
|
return;
|
|
|
1460
|
}
|
|
|
1461
|
|
|
|
1462
|
Uri result = null;
|
|
|
1463
|
if (requestCode == FILE_CAMERA_RESULT_CODE) {
|
|
|
1464
|
if (null != data && null != data.getData()) {
|
|
|
1465
|
result = data.getData();
|
|
|
1466
|
}
|
|
|
1467
|
if (result == null && ImageUitl.hasFile(cameraFilePath)) {
|
|
|
1468
|
result = Uri.fromFile(new File(cameraFilePath));
|
|
|
1469
|
}
|
|
|
1470
|
|
|
1365
|
1471
|
if (mOpenFileWebChromeClient.mFilePathCallbacks != null) {
|
|
1366
|
|
Uri result = intent == null || resultCode != Activity.RESULT_OK ? null
|
|
1367
|
|
: intent.getData();
|
|
1368
|
|
if (result != null) {
|
|
1369
|
|
String path = MediaUtility.getPath(getApplicationContext(),
|
|
1370
|
|
result);
|
|
1371
|
|
Uri uri = Uri.fromFile(new File(path));
|
|
1372
|
|
mOpenFileWebChromeClient.mFilePathCallbacks
|
|
1373
|
|
.onReceiveValue(new Uri[]{uri});
|
|
1374
|
|
} else {
|
|
1375
|
|
mOpenFileWebChromeClient.mFilePathCallbacks
|
|
1376
|
|
.onReceiveValue(null);
|
|
1377
|
|
}
|
|
|
1472
|
mOpenFileWebChromeClient.mFilePathCallbacks.onReceiveValue(new Uri[]{result});
|
|
|
1473
|
mOpenFileWebChromeClient.mFilePathCallbacks = null;
|
|
|
1474
|
} else if (mOpenFileWebChromeClient.mFilePathCallback != null) {
|
|
|
1475
|
mOpenFileWebChromeClient.mFilePathCallback.onReceiveValue(result);
|
|
|
1476
|
mOpenFileWebChromeClient.mFilePathCallback = null;
|
|
|
1477
|
}
|
|
|
1478
|
} else if (requestCode == FILE_CHOOSER_RESULT_CODE) {
|
|
|
1479
|
if (data != null) {
|
|
|
1480
|
result = data.getData();
|
|
|
1481
|
}
|
|
|
1482
|
if (mOpenFileWebChromeClient.mFilePathCallbacks != null) {
|
|
|
1483
|
onActivityResultCallbacks(data);
|
|
|
1484
|
} else if (mOpenFileWebChromeClient.mFilePathCallback != null) {
|
|
|
1485
|
mOpenFileWebChromeClient.mFilePathCallback.onReceiveValue(result);
|
|
|
1486
|
mOpenFileWebChromeClient.mFilePathCallback = null;
|
|
1378
|
1487
|
}
|
|
|
1488
|
}
|
|
|
1489
|
}
|
|
1379
|
1490
|
|
|
1380
|
|
mOpenFileWebChromeClient.mFilePathCallback = null;
|
|
|
1491
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
|
|
1492
|
private void onActivityResultCallbacks(Intent intent) {
|
|
|
1493
|
Uri[] results = null;
|
|
|
1494
|
if (intent != null) {
|
|
|
1495
|
String dataString = intent.getDataString();
|
|
|
1496
|
ClipData clipData = intent.getClipData();
|
|
|
1497
|
if (clipData != null) {
|
|
|
1498
|
results = new Uri[clipData.getItemCount()];
|
|
|
1499
|
for (int i=0;i<clipData.getItemCount();i++) {
|
|
|
1500
|
ClipData.Item item = clipData.getItemAt(i);
|
|
|
1501
|
results[i] = item.getUri();
|
|
|
1502
|
}
|
|
|
1503
|
}
|
|
|
1504
|
if (dataString != null) {
|
|
|
1505
|
results = new Uri[] {Uri.parse(dataString)};
|
|
|
1506
|
}
|
|
|
1507
|
mOpenFileWebChromeClient.mFilePathCallbacks.onReceiveValue(results);
|
|
1381
|
1508
|
mOpenFileWebChromeClient.mFilePathCallbacks = null;
|
|
1382
|
1509
|
}
|
|
1383
|
1510
|
}
|
|
|
@ -7,11 +7,13 @@ import android.graphics.Bitmap;
|
|
7
|
7
|
import android.graphics.BitmapFactory;
|
|
8
|
8
|
import android.graphics.Matrix;
|
|
9
|
9
|
import android.net.Uri;
|
|
|
10
|
import android.os.Environment;
|
|
10
|
11
|
import android.util.Log;
|
|
11
|
12
|
|
|
12
|
13
|
import java.io.ByteArrayInputStream;
|
|
13
|
14
|
import java.io.ByteArrayOutputStream;
|
|
14
|
15
|
import java.io.File;
|
|
|
16
|
import java.io.IOException;
|
|
15
|
17
|
import java.io.InputStream;
|
|
16
|
18
|
|
|
17
|
19
|
/**
|
|
|
@ -396,4 +398,45 @@ public class ImageUitl {
|
|
396
|
398
|
bitmap = compressImage(bitmap, 100);//质量压缩
|
|
397
|
399
|
return bitmap;
|
|
398
|
400
|
}
|
|
|
401
|
|
|
|
402
|
public static boolean hasSdcard() {
|
|
|
403
|
if (android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
|
|
404
|
return true;
|
|
|
405
|
} else {
|
|
|
406
|
return false;
|
|
|
407
|
}
|
|
|
408
|
}
|
|
|
409
|
|
|
|
410
|
public static String getPath(String path) {
|
|
|
411
|
File f = new File(path);
|
|
|
412
|
if (!f.exists()) {
|
|
|
413
|
f.mkdirs();
|
|
|
414
|
}
|
|
|
415
|
return f.getAbsolutePath();
|
|
|
416
|
}
|
|
|
417
|
|
|
|
418
|
public static File getFile(String path) {
|
|
|
419
|
File f = new File(path);
|
|
|
420
|
if (!f.exists()) {
|
|
|
421
|
try {
|
|
|
422
|
f.createNewFile();
|
|
|
423
|
} catch (IOException e) {
|
|
|
424
|
e.printStackTrace();
|
|
|
425
|
}
|
|
|
426
|
}
|
|
|
427
|
return f;
|
|
|
428
|
}
|
|
|
429
|
|
|
|
430
|
public static boolean hasFile(String path) {
|
|
|
431
|
try {
|
|
|
432
|
File f = new File(path);
|
|
|
433
|
if (!f.exists()) {
|
|
|
434
|
return false;
|
|
|
435
|
}
|
|
|
436
|
} catch (Exception e) {
|
|
|
437
|
return false;
|
|
|
438
|
}
|
|
|
439
|
return true;
|
|
|
440
|
}
|
|
|
441
|
|
|
399
|
442
|
}
|
|
|
@ -1,72 +0,0 @@
|
|
1
|
|
package com.electric.chargingpile.util;
|
|
2
|
|
|
|
3
|
|
import android.app.Activity;
|
|
4
|
|
import android.content.Intent;
|
|
5
|
|
import android.net.Uri;
|
|
6
|
|
import android.webkit.ValueCallback;
|
|
7
|
|
import android.webkit.WebChromeClient;
|
|
8
|
|
import android.webkit.WebView;
|
|
9
|
|
|
|
10
|
|
/**
|
|
11
|
|
* Created by Demon on 16/7/19.
|
|
12
|
|
*/
|
|
13
|
|
|
|
14
|
|
public class OpenFileWebChromeClient extends WebChromeClient {
|
|
15
|
|
public static final int REQUEST_FILE_PICKER = 1;
|
|
16
|
|
public ValueCallback<Uri> mFilePathCallback;
|
|
17
|
|
public ValueCallback<Uri[]> mFilePathCallbacks;
|
|
18
|
|
Activity mContext;
|
|
19
|
|
|
|
20
|
|
public OpenFileWebChromeClient(Activity mContext) {
|
|
21
|
|
super();
|
|
22
|
|
this.mContext = mContext;
|
|
23
|
|
}
|
|
24
|
|
|
|
25
|
|
// Android < 3.0 调用这个方法
|
|
26
|
|
public void openFileChooser(ValueCallback<Uri> filePathCallback) {
|
|
27
|
|
mFilePathCallback = filePathCallback;
|
|
28
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
29
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
30
|
|
intent.setType("*/*");
|
|
31
|
|
mContext.startActivityForResult(Intent.createChooser(intent, "File Chooser"),
|
|
32
|
|
REQUEST_FILE_PICKER);
|
|
33
|
|
}
|
|
34
|
|
|
|
35
|
|
// 3.0 + 调用这个方法
|
|
36
|
|
public void openFileChooser(ValueCallback filePathCallback,
|
|
37
|
|
String acceptType) {
|
|
38
|
|
mFilePathCallback = filePathCallback;
|
|
39
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
40
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
41
|
|
intent.setType("*/*");
|
|
42
|
|
mContext.startActivityForResult(Intent.createChooser(intent, "File Chooser"),
|
|
43
|
|
REQUEST_FILE_PICKER);
|
|
44
|
|
}
|
|
45
|
|
|
|
46
|
|
// / js上传文件的<input type="file" name="fileField" id="fileField" />事件捕获
|
|
47
|
|
// Android > 4.1.1 调用这个方法
|
|
48
|
|
public void openFileChooser(ValueCallback<Uri> filePathCallback,
|
|
49
|
|
String acceptType, String capture) {
|
|
50
|
|
mFilePathCallback = filePathCallback;
|
|
51
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
52
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
53
|
|
intent.setType("*/*");
|
|
54
|
|
mContext.startActivityForResult(Intent.createChooser(intent, "File Chooser"),
|
|
55
|
|
REQUEST_FILE_PICKER);
|
|
56
|
|
}
|
|
57
|
|
|
|
58
|
|
@Override
|
|
59
|
|
public boolean onShowFileChooser(WebView webView,
|
|
60
|
|
ValueCallback<Uri[]> filePathCallback,
|
|
61
|
|
WebChromeClient.FileChooserParams fileChooserParams) {
|
|
62
|
|
mFilePathCallbacks = filePathCallback;
|
|
63
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
64
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
65
|
|
intent.setType("*/*");
|
|
66
|
|
mContext.startActivityForResult(Intent.createChooser(intent, "File Chooser"),
|
|
67
|
|
REQUEST_FILE_PICKER);
|
|
68
|
|
return true;
|
|
69
|
|
}
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
}
|
|
|
@ -13,6 +13,10 @@
|
|
13
|
13
|
<item>"Android provides a rich application framework that allows you to build innovative apps and games for mobile devices in a Java language environment. The documents listed in the left navigation provide details about how to build apps using Android's various APIs."</item>
|
|
14
|
14
|
<item>"test"</item>
|
|
15
|
15
|
</string-array>
|
|
|
16
|
<string-array name="photo">
|
|
|
17
|
<item>拍照</item>
|
|
|
18
|
<item>照片图库</item>
|
|
|
19
|
</string-array>
|
|
16
|
20
|
|
|
17
|
21
|
<string-array name="color_arr">
|
|
18
|
22
|
<item>@color/lvse</item>
|