|
@@ -3,22 +3,28 @@ package com.ynstkz.shitu.android.activity;
|
|
|
import android.Manifest;
|
|
|
import android.annotation.SuppressLint;
|
|
|
import android.app.Activity;
|
|
|
+import android.content.ContentResolver;
|
|
|
+import android.content.ContentUris;
|
|
|
+import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.content.pm.PackageManager;
|
|
|
import android.content.res.Configuration;
|
|
|
+import android.database.Cursor;
|
|
|
import android.net.Uri;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Environment;
|
|
|
+import android.provider.DocumentsContract;
|
|
|
import android.provider.MediaStore;
|
|
|
import android.support.annotation.NonNull;
|
|
|
import android.support.annotation.Nullable;
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
-import android.support.v4.content.ContextCompat;
|
|
|
+import android.support.v4.content.FileProvider;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
import android.view.KeyEvent;
|
|
|
import android.view.View;
|
|
|
+import android.webkit.JavascriptInterface;
|
|
|
import android.webkit.ValueCallback;
|
|
|
import android.webkit.WebChromeClient;
|
|
|
import android.webkit.WebSettings;
|
|
@@ -29,8 +35,10 @@ import android.widget.RelativeLayout;
|
|
|
import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
+import com.ynstkz.shitu.android.BuildConfig;
|
|
|
import com.ynstkz.shitu.android.R;
|
|
|
import com.ynstkz.shitu.android.base.TitleBarActivity;
|
|
|
+import com.ynstkz.shitu.android.utils.FileUtils;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
@@ -38,6 +46,8 @@ import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
|
|
|
import butterknife.Bind;
|
|
|
+import top.zibin.luban.Luban;
|
|
|
+import top.zibin.luban.OnCompressListener;
|
|
|
|
|
|
/**
|
|
|
* 作者:fuchangle on 2018/5/4 10:43
|
|
@@ -62,6 +72,8 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
private ValueCallback<Uri[]> mUMA;
|
|
|
private final static int FCR = 1;
|
|
|
|
|
|
+ private File cameraFile = null;
|
|
|
+
|
|
|
@Override
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
@@ -78,8 +90,21 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
tvTitle.setText(getString(R.string.app_name));
|
|
|
}
|
|
|
|
|
|
- if (Build.VERSION.SDK_INT >= 23 && (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
|
|
|
- ActivityCompat.requestPermissions(ApplyWebActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
|
|
|
+// if (Build.VERSION.SDK_INT >= 23 && (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|
|
|
+// || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
|
|
|
+// || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+// ActivityCompat.requestPermissions(ApplyWebActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
|
|
+// Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE }, 1);
|
|
|
+// }
|
|
|
+
|
|
|
+ if (ActivityCompat.checkSelfPermission(ApplyWebActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ //权限发生了改变 true // false 小米
|
|
|
+ ActivityCompat.requestPermissions(ApplyWebActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ActivityCompat.checkSelfPermission(ApplyWebActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ //权限发生了改变 true // false 小米
|
|
|
+ ActivityCompat.requestPermissions(ApplyWebActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1000);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -90,6 +115,10 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
|
|
|
private void initData() {
|
|
|
url = getIntent().getStringExtra("url");
|
|
|
+ if(TextUtils.isEmpty(url)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Log.e("okhttp", url);
|
|
|
initWebView();
|
|
|
}
|
|
|
|
|
@@ -117,6 +146,7 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
webSettings.setDomStorageEnabled(true);
|
|
|
webSettings.setLoadWithOverviewMode(true);
|
|
|
webSettings.setUseWideViewPort(true);
|
|
|
+ webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
|
|
webSettings.setMixedContentMode(0);
|
|
@@ -126,8 +156,12 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
} else if (Build.VERSION.SDK_INT < 19) {
|
|
|
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
|
|
|
}
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
+ webView.setWebContentsDebuggingEnabled(true);
|
|
|
+ }
|
|
|
webView.setWebViewClient(new Callback());
|
|
|
webView.loadUrl(url);
|
|
|
+ webView.addJavascriptInterface(new JSCallBack(), "js_callback");
|
|
|
webView.setWebChromeClient(new WebChromeClient() {
|
|
|
//For Android 3.0+
|
|
|
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
|
|
@@ -168,16 +202,15 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
mUMA = filePathCallback;
|
|
|
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
if (takePictureIntent.resolveActivity(ApplyWebActivity.this.getPackageManager()) != null) {
|
|
|
- File photoFile = null;
|
|
|
try {
|
|
|
- photoFile = createImageFile();
|
|
|
+ cameraFile = createImageFile();
|
|
|
takePictureIntent.putExtra("PhotoPath", mCM);
|
|
|
} catch (IOException ex) {
|
|
|
Log.e(TAG, "Image file creation failed", ex);
|
|
|
}
|
|
|
- if (photoFile != null) {
|
|
|
- mCM = "file:" + photoFile.getAbsolutePath();
|
|
|
- takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
|
|
|
+ if (cameraFile != null) {
|
|
|
+ mCM = "file:" + cameraFile.getAbsolutePath();
|
|
|
+ takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));
|
|
|
} else {
|
|
|
takePictureIntent = null;
|
|
|
}
|
|
@@ -194,7 +227,7 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
|
|
|
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
|
|
|
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
|
|
|
- chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
|
|
|
+ chooserIntent.putExtra(Intent.EXTRA_TITLE, "选择文件");
|
|
|
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
|
|
|
startActivityForResult(chooserIntent, FCR);
|
|
|
return true;
|
|
@@ -202,6 +235,24 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * js回调
|
|
|
+ */
|
|
|
+ public class JSCallBack {
|
|
|
+ @JavascriptInterface
|
|
|
+ public void applysuccess(){
|
|
|
+ runOnUiThread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Intent intent = new Intent(ApplyWebActivity.this, MineSignUpActivity.class);
|
|
|
+ intent.putExtra("currentIndex", 1);
|
|
|
+ startActivity(intent);
|
|
|
+ finish();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public class Callback extends WebViewClient {
|
|
|
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
|
|
Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
|
|
@@ -216,6 +267,40 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
return File.createTempFile(imageFileName, ".jpg", storageDir);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 图片压缩
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ private void compressImage(File file){
|
|
|
+ if(file == null) {
|
|
|
+ showToast("Please choose an image!");
|
|
|
+ } else {
|
|
|
+ Luban.with(this)
|
|
|
+ .load(file)
|
|
|
+ .ignoreBy(1024)
|
|
|
+ .setTargetDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath())
|
|
|
+ .setCompressListener(new OnCompressListener() {
|
|
|
+ @Override
|
|
|
+ public void onStart() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess(File file) {
|
|
|
+ mCM = "file:" + file.getAbsolutePath();
|
|
|
+ Uri[] results = new Uri[]{Uri.parse(mCM)};
|
|
|
+ mUMA.onReceiveValue(results);
|
|
|
+ mUMA = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(Throwable e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }).launch();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
|
|
|
if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
|
@@ -223,10 +308,10 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
case KeyEvent.KEYCODE_BACK:
|
|
|
if (webView.canGoBack()) {
|
|
|
webView.goBack();
|
|
|
+ return true;
|
|
|
} else {
|
|
|
finish();
|
|
|
}
|
|
|
- return true;
|
|
|
}
|
|
|
}
|
|
|
return super.onKeyDown(keyCode, event);
|
|
@@ -251,12 +336,43 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
if (intent == null || intent.getData() == null) {
|
|
|
//Capture Photo if no image available
|
|
|
if (mCM != null) {
|
|
|
+ if(cameraFile != null && cameraFile.length() > 1024 * 1024) { //图片大于1M
|
|
|
+ compressImage(cameraFile);
|
|
|
+ return;
|
|
|
+ }
|
|
|
results = new Uri[]{Uri.parse(mCM)};
|
|
|
}
|
|
|
} else {
|
|
|
String dataString = intent.getDataString();
|
|
|
if (dataString != null) {
|
|
|
- results = new Uri[]{Uri.parse(dataString)};
|
|
|
+ Uri uri= Uri.parse(dataString);
|
|
|
+ String imagePath = null;
|
|
|
+ if (DocumentsContract.isDocumentUri(this, uri)) {
|
|
|
+ String docId = DocumentsContract.getDocumentId(uri);
|
|
|
+ if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
|
|
|
+ //Log.d(TAG, uri.toString());
|
|
|
+ String id = docId.split(":")[1];
|
|
|
+ String selection = MediaStore.Images.Media._ID + "=" + id;
|
|
|
+ imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
|
|
|
+ } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
|
|
|
+ //Log.d(TAG, uri.toString());
|
|
|
+ Uri contentUri = ContentUris.withAppendedId(
|
|
|
+ Uri.parse("content://downloads/public_downloads"),
|
|
|
+ Long.valueOf(docId));
|
|
|
+ imagePath = getImagePath(contentUri, null);
|
|
|
+ }
|
|
|
+ } else if ("content".equalsIgnoreCase(uri.getScheme())) {
|
|
|
+ //Log.d(TAG, "content: " + uri.toString());
|
|
|
+ imagePath = getImagePath(uri, null);
|
|
|
+ }
|
|
|
+ if(!TextUtils.isEmpty(imagePath)) {
|
|
|
+ File photoImage = new File(imagePath);
|
|
|
+ if(photoImage.length() > 1024 * 1024) {
|
|
|
+ compressImage(photoImage);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ results = new Uri[]{uri};
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -274,4 +390,16 @@ public class ApplyWebActivity extends TitleBarActivity {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ private String getImagePath(Uri uri, String selection) {
|
|
|
+ String path = null;
|
|
|
+ Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
|
|
|
+ if (cursor != null) {
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
+ path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
|
|
|
+ }
|
|
|
+
|
|
|
+ cursor.close();
|
|
|
+ }
|
|
|
+ return path;
|
|
|
+ }
|
|
|
}
|