Commit fbe78c09 by gao.chao

web页面(获取定位未完成)

parent 1882698c
...@@ -25,5 +25,7 @@ dependencies { ...@@ -25,5 +25,7 @@ dependencies {
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
api "com.tencent.bugly:crashreport_upgrade:1.3.4"
api 'com.tencent.bugly:nativecrashreport:3.6.0.1'
api project(':fastDevelop') api project(':fastDevelop')
} }
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppThemeNoActionBar" >
<activity android:name=".MainActivity"> <activity android:name=".MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
<activity android:name=".DownLoadAPKActivity"/> <activity android:name=".DownLoadAPKActivity"/>
<activity android:name=".FragmentInteractiveActivity"/> <activity android:name=".FragmentInteractiveActivity"/>
<activity android:name=".FragmentInteractiveActivity2"/> <activity android:name=".FragmentInteractiveActivity2"/>
<activity android:name=".PageActivity"/>
</application> </application>
<!-- 配置APP ID --> <!-- 配置APP ID -->
......
...@@ -36,6 +36,13 @@ public class MainActivity extends BaseActivity { ...@@ -36,6 +36,13 @@ public class MainActivity extends BaseActivity {
gotoActivity(FunctionActivity.class); gotoActivity(FunctionActivity.class);
} }
})); }));
list.add(new ItemBean("常用页面", new View.OnClickListener() {
@Override
public void onClick(View v) {
gotoActivity(PageActivity.class);
}
}));
listView.setAdapter(new RecyclerView.Adapter<MyViewHolder>() { listView.setAdapter(new RecyclerView.Adapter<MyViewHolder>() {
@NonNull @NonNull
@Override @Override
......
package com.mayi.demo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.commonpage.WebActivity;
import com.mayi.fastdevelop.view.OnMultiClickListener;
public class PageActivity extends BaseActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page);
findViewById(R.id.b1).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
Bundle bundle=new Bundle();
bundle.putString("url","https://customer.kujiatech.com/#/?token=8427db8972dd09105a00d62d8bc7cfd5&username=18001874470&v="+System.currentTimeMillis());
gotoActivity(WebActivity.class,bundle);
}
});
findViewById(R.id.b2).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
Bundle bundle=new Bundle();
bundle.putString("url","https://customer.kujiatech.com/#/?token=8427db8972dd09105a00d62d8bc7cfd5" +
"&username=18001874470&v="+System.currentTimeMillis()+"&isNeedBackButton=true&userAppTitle=潜客");
gotoActivity(WebActivity.class,bundle);
}
});
findViewById(R.id.b3).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
gotoActivity(DownLoadAPKActivity.class);
}
});
findViewById(R.id.b4).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
gotoActivity(FragmentInteractiveActivity.class);
}
});
findViewById(R.id.b5).setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
gotoActivity(FragmentInteractiveActivity2.class);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="web页面" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="web页面,显示标题" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载安装APP" />
<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity与Fragment之间交互\n可以左右滑动" />
<Button
android:id="@+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity与Fragment之间交互\n不可以左右滑动" />
</LinearLayout>
</ScrollView>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout <com.mayi.fastdevelop.view.CustomTitleBar
android:id="@+id/title1"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="@dimen/im_dp_50"
android:orientation="vertical"> android:layout_marginTop="@dimen/im_dp_20" />
<com.mayi.fastdevelop.view.CustomTitleBar
android:id="@+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/im_dp_20" />
<com.mayi.fastdevelop.view.CustomTitleBar
android:id="@+id/title2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/im_dp_20" />
<com.mayi.fastdevelop.view.CustomTitleBar
android:id="@+id/title3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/im_dp_20" />
<com.mayi.fastdevelop.view.CustomTitleBar <com.mayi.fastdevelop.view.CustomTitleBar
android:id="@+id/title4" android:id="@+id/title2"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="@dimen/im_dp_50"
android:layout_marginTop="@dimen/im_dp_20" /> android:layout_marginTop="@dimen/im_dp_20" />
<com.mayi.fastdevelop.view.CustomTitleBar <com.mayi.fastdevelop.view.CustomTitleBar
android:id="@+id/title5" android:id="@+id/title3"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="@dimen/im_dp_50"
android:layout_marginTop="@dimen/im_dp_20" /> android:layout_marginTop="@dimen/im_dp_20" />
<com.mayi.fastdevelop.view.CustomTitleBar
android:id="@+id/title4"
android:layout_width="match_parent"
android:layout_height="@dimen/im_dp_50"
android:layout_marginTop="@dimen/im_dp_20" />
</LinearLayout> <com.mayi.fastdevelop.view.CustomTitleBar
android:id="@+id/title5"
android:layout_width="match_parent"
android:layout_height="@dimen/im_dp_50"
android:layout_marginTop="@dimen/im_dp_20" />
</ScrollView> </LinearLayout>
\ No newline at end of file
...@@ -29,8 +29,6 @@ dependencies { ...@@ -29,8 +29,6 @@ dependencies {
api 'com.yanzhenjie:permission:2.0.0-rc12'//权限申请 api 'com.yanzhenjie:permission:2.0.0-rc12'//权限申请
api 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'//下拉刷新,加载更多 api 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'//下拉刷新,加载更多
api 'com.youth.banner:banner:1.4.10' //图片轮播控件 api 'com.youth.banner:banner:1.4.10' //图片轮播控件
api "com.tencent.bugly:crashreport_upgrade:1.3.4"
api 'com.tencent.bugly:nativecrashreport:3.6.0.1'
api 'com.squareup.okhttp3:okhttp:3.10.0' api 'com.squareup.okhttp3:okhttp:3.10.0'
api 'com.squareup.picasso:picasso:2.5.2'//图片加载 api 'com.squareup.picasso:picasso:2.5.2'//图片加载
api 'com.tencent.bugly:crashreport_upgrade:1.3.4' api 'com.tencent.bugly:crashreport_upgrade:1.3.4'
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
package="com.mayi.fastdevelop"> package="com.mayi.fastdevelop">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
...@@ -12,4 +12,21 @@ ...@@ -12,4 +12,21 @@
<uses-permission <uses-permission
android:name="android.permission.READ_LOGS" android:name="android.permission.READ_LOGS"
tools:ignore="ProtectedPermissions" /> tools:ignore="ProtectedPermissions" />
<application>
<activity android:name=".commonpage.WebActivity" />
<!-- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mayi.fastdevelop"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest> </manifest>
...@@ -24,7 +24,7 @@ import com.mayi.fastdevelop.view.installapk.AppUpgradeManager; ...@@ -24,7 +24,7 @@ import com.mayi.fastdevelop.view.installapk.AppUpgradeManager;
/** /**
* 下载安装Activity * 下载安装Activity
*/ */
public abstract class UpdateDownLoadAPKActvity extends BaseActivity { public class UpdateDownLoadAPKActvity extends BaseActivity {
private ApkInstallReceiver receiver; private ApkInstallReceiver receiver;
......
package com.mayi.fastdevelop.commonpage;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import com.mayi.fastdevelop.R;
import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.comnon.RequestCode;
import com.mayi.fastdevelop.util.BitmapUtil;
import com.mayi.fastdevelop.util.DialogUtils;
import com.mayi.fastdevelop.view.CustomTitleBar;
import com.mayi.fastdevelop.view.dialog.BaseDialog;
import com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
public class WebActivity extends BaseActivity {
private WebView webView;
private CustomTitleBar title;
private Handler handler;
private ValueCallback<Uri> uploadMessage;
private ValueCallback<Uri[]> uploadMessageAboveL;
private String mCurrentPhotoPath;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
webView = findViewById(R.id.webview);
title = findViewById(R.id.title);
title.setOnClickLeftViewListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webPageBack();
}
});
handler = new Handler();
initWebView();
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
}
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissionsCallback geolocationPermissionsCallback) {
geolocationPermissionsCallback.invoke(origin, true, false);
super.onGeolocationPermissionsShowPrompt(origin, geolocationPermissionsCallback);
}
//For Android >= 5.0
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
uploadMessageAboveL = filePathCallback;
uploadPicture();
return true;
}
//For Android >= 4.1
public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {
uploadMessage = valueCallback;
uploadPicture();
}
});
initView();
}
/**
* web返回上一页
*/
private void webPageBack() {
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}
private void initView() {
String url = getIntent().getStringExtra("url");
if (TextUtils.isEmpty(url)) {
return;
}
if (url.contains("isNeedBackButton")) {
title.setVisibility(View.VISIBLE);
} else {
title.setVisibility(View.GONE);
}
if (url.contains("userAppTitle=")) {
int start = url.indexOf("userAppTitle=");
String text = null;
if (url.indexOf("&", start) != -1) {
text = url.substring(start + "userAppTitle=".length(), url.indexOf("&", start));
} else {
text = url.substring(start + "userAppTitle=".length());
}
String keyWord = "";
try {
keyWord = URLDecoder.decode(text, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
title.setTextCenter(keyWord);
}
webView.loadUrl(url);
}
/**
* 选择相机或者相册
*/
public void uploadPicture() {
BaseDialog dialog = DialogUtils.showSelectPicturesDialog(this, new DialogUtils.DefaultDialogListener() {
@Override
public void onClickLeft() {
//请求拍照权限
if (ActivityCompat.checkSelfPermission(WebActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
takePhoto();
} else {
ActivityCompat.requestPermissions(WebActivity.this, new String[]{Manifest.permission.CAMERA}, RequestCode.REQUEST_CODE_PERMISSION_CAMERA);
}
}
@Override
public void onClickRight() {
chooseAlbumPic();
}
});
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//一定要返回null,否则<input type='file'>
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
if (uploadMessageAboveL != null) {
uploadMessageAboveL.onReceiveValue(null);
uploadMessageAboveL = null;
}
}
});
}
/**
* 拍照
*/
public void takePhoto() {
StringBuilder fileName = new StringBuilder();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileName.append(System.currentTimeMillis()).append("_upload.jpg");
File tempFile = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName.toString());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uri = FileProvider.getUriForFile(this, "com.mayi.fastdevelop", tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
} else {
Uri uri = Uri.fromFile(tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
mCurrentPhotoPath = tempFile.getAbsolutePath();
startActivityForResult(intent, RequestCode.REQUEST_CODE_CAMERA);
}
/**
* 选择相册照片
*/
public void chooseAlbumPic() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "Image Chooser"), RequestCode.REQUEST_CODE_ALBUM);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (webView.canGoBack()) {
webView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
//app调用web方法
public void appCallJs(String method, String data) {
webView.loadUrl("javascript:appCallJs.invoking;(" + method + ","+data+")");
}
@JavascriptInterface
public void invoking(final String method, final String data) {
handler.post(new Runnable() {
@Override
public void run() {
if (TextUtils.equals("goCallPhone", method)) {//去打电话
Intent intent = new Intent(Intent.ACTION_DIAL);
Uri uri = Uri.parse("tel:" + data);
intent.setData(uri);
startActivity(intent);
} else if (TextUtils.equals("backPage", method)) {//返回上一页
webPageBack();
} else if (TextUtils.equals("goPage", method)) {//跳转原生页面
try {
JSONObject jsonObject = new JSONObject(data);
goPage(jsonObject.optString("page"), jsonObject.optString("parameter"));
} catch (JSONException e) {
e.printStackTrace();
}
} else if (TextUtils.equals("openDebug", method)) {//开启Debug模式
WebView.setWebContentsDebuggingEnabled(true);
} else if (TextUtils.equals("goBrowser", method)) {//跳转浏览器
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse(data);
intent.setData(content_url);
startActivity(intent);
}
}
});
}
/**
* 跳转原生页面
* 待扩展
*
* @param s
* @param parameter
*/
public void goPage(String s, String parameter) {
}
private void initWebView() {
WebSettings settings = webView.getSettings();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
settings.setMixedContentMode(2);
}
settings.setBlockNetworkImage(false);
settings.setSupportMultipleWindows(false);
settings.setDefaultTextEncodingName("utf-8");
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
settings.setAllowFileAccess(true);
settings.setAppCacheEnabled(true);
webView.setHorizontalScrollBarEnabled(false);//水平不显示
webView.setVerticalScrollBarEnabled(false); //垂直不显示
settings.setUseWideViewPort(true); //适应屏幕
settings.setLoadWithOverviewMode(true);
settings.setSupportZoom(true); //支持屏幕缩放
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false); //不显示webview缩放按钮
settings.setDatabaseEnabled(true);
String dir = getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
settings.setGeolocationEnabled(true); //启用地理定位
settings.setGeolocationDatabasePath(dir); //设置定位的数据库路径
settings.setDomStorageEnabled(true); //最重要的方法,一定要设置,这就是出不来的主要原因
webView.addJavascriptInterface(this, "jsCallApp");
}
private void uploadImage(Uri imageUri) {
if (uploadMessage != null) {
uploadMessage.onReceiveValue(imageUri);
uploadMessage = null;
}
if (uploadMessageAboveL != null) {
if (imageUri == null) {
uploadMessageAboveL.onReceiveValue(null);
} else {
uploadMessageAboveL.onReceiveValue(new Uri[]{imageUri});
}
uploadMessageAboveL = null;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RequestCode.REQUEST_CODE_ALBUM || requestCode == RequestCode.REQUEST_CODE_CAMERA) {
if (uploadMessage == null && uploadMessageAboveL == null) {
return;
}
if (resultCode != RESULT_OK) {//取消拍照或者图片选择时
uploadImage(null); //一定要返回null,否则<input file> 就是没有反应
} else if (resultCode == RESULT_OK) { //拍照成功和选取照片时
Uri imageUri = null;
switch (requestCode) {
case RequestCode.REQUEST_CODE_ALBUM:
if (data != null) {
imageUri = data.getData();
}
uploadImage(imageUri);
break;
case RequestCode.REQUEST_CODE_CAMERA:
zoomsInPhoto();
showLoadingDialog();
break;
}
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 缩小照片尺寸
*/
private void zoomsInPhoto() {
AsyncTask asyncTask = new AsyncTask() {
@Override
protected Object doInBackground(Object[] objects) {
if (!TextUtils.isEmpty(mCurrentPhotoPath)) {
BitmapUtil.zoomsInImageFile(mCurrentPhotoPath, mCurrentPhotoPath,
Bitmap.CompressFormat.JPEG, 900, 100);
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
Uri uri = null;
if (!TextUtils.isEmpty(mCurrentPhotoPath)) {
File file = new File(mCurrentPhotoPath);
uri = Uri.fromFile(file);
}
uploadImage(uri);
dismssLoadingDialog();
}
};
asyncTask.execute();
}
}
...@@ -2,4 +2,7 @@ package com.mayi.fastdevelop.comnon; ...@@ -2,4 +2,7 @@ package com.mayi.fastdevelop.comnon;
public class RequestCode { public class RequestCode {
public static final int REQUEST_CODE_APP_INSTALL = 0x00; public static final int REQUEST_CODE_APP_INSTALL = 0x00;
public static final int REQUEST_CODE_PERMISSION_CAMERA = 0x01;
public static final int REQUEST_CODE_CAMERA = 0x02;
public static final int REQUEST_CODE_ALBUM = 0x03;
} }
...@@ -58,11 +58,9 @@ public class BitmapUtil { ...@@ -58,11 +58,9 @@ public class BitmapUtil {
} else if (quality > 100) { } else if (quality > 100) {
quality = 100; quality = 100;
} }
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inJustDecodeBounds = false; options.inJustDecodeBounds = false;
options.inSampleSize = 1;
Bitmap inBitmap = BitmapFactory.decodeFile(in, options); Bitmap inBitmap = BitmapFactory.decodeFile(in, options);
File of = new File(out); File of = new File(out);
...@@ -94,6 +92,62 @@ public class BitmapUtil { ...@@ -94,6 +92,62 @@ public class BitmapUtil {
return false; return false;
} }
/**
*根据宽度对图片进行缩小,再压缩,最好保存到文件
*
* @param in 需要压缩的图片绝对路径
* @param out 压缩后保存的文件绝对路径
* @param format 输出格式(默认JEPG):Bitmap.CompressFormat.JEPG,Bitmap.CompressFormat.PNG,Bitmap.CompressFormat.WEBP
* @param maxwidth 图片最大宽
* @return 成功true, 不成功false
*/
public static boolean zoomsInImageFile(String in, String out, Bitmap.CompressFormat format, int maxwidth,int quality) {
if (!new File(in).exists()) {
return false;
}
if (format == null) {
format = Bitmap.CompressFormat.JPEG;
}
Bitmap inBitmap = loadBitmapFromFile(in,maxwidth);
File of = new File(out);
FileOutputStream fos = null;
try {
if (!of.exists()) {
boolean created = of.createNewFile();
if (!created) {
return false;
}
}
fos = new FileOutputStream(of);
inBitmap.compress(format, quality, fos);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inBitmap != null) {
inBitmap.recycle();
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
private static int getinSampleSize(int bitmapHeight, int height) {
double proportion = ArithUtil.div(bitmapHeight, height);
if (proportion >= 2) {
return (int) proportion;
}
return 2;
}
/** /**
* 根据宽高加载图片 * 根据宽高加载图片
* *
...@@ -190,7 +244,6 @@ public class BitmapUtil { ...@@ -190,7 +244,6 @@ public class BitmapUtil {
* @return bitmap * @return bitmap
*/ */
public static Bitmap loadBitmapFromFile(String path) { public static Bitmap loadBitmapFromFile(String path) {
return BitmapFactory.decodeFile(path); return BitmapFactory.decodeFile(path);
} }
...@@ -298,7 +351,6 @@ public class BitmapUtil { ...@@ -298,7 +351,6 @@ public class BitmapUtil {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
} }
......
...@@ -68,6 +68,41 @@ public class DialogUtils { ...@@ -68,6 +68,41 @@ public class DialogUtils {
.show(); .show();
} }
/**
* 选择图片dialog
* @param context
* @param dialogListener
* @return
*/
public static BaseDialog showSelectPicturesDialog(Context context, final DefaultDialogListener dialogListener) {
return new BaseDialog(context, R.layout.dialog_select_pictures) {
@Override
public void convert(DialogViewHolder holder) {
holder.setOnClick(R.id.tv_album, new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
if (dialogListener != null) {
dialogListener.onClickRight();
}
}
});
holder.setOnClick(R.id.tv_photograph, new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
if (dialogListener != null) {
dialogListener.onClickLeft();
}
}
});
}
}.setCanceledOnTouchOutside(true)
.setCancelAble(true)
.show();
}
public interface TipsDialogListener { public interface TipsDialogListener {
void onClickOK(); void onClickOK();
} }
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.mayi.fastdevelop.view.CustomTitleBar
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="@dimen/im_dp_50" />
<com.tencent.smtt.sdk.WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/bg_dialog_shape">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/im_dp_15"
android:text="请选择图片上传方式"
android:textColor="@color/color_333333"
android:textSize="@dimen/txt_size_32px" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/im_dp_0.5"
android:layout_below="@+id/tv_title"
android:background="@color/color_666666" />
<TextView
android:id="@+id/tv_album"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_title"
android:layout_marginLeft="@dimen/im_dp_10"
android:padding="@dimen/im_dp_10"
android:text="@string/album"
android:textColor="@color/color_333333"
android:textSize="@dimen/txt_size_32px" />
<TextView
android:id="@+id/tv_photograph"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_album"
android:layout_marginLeft="@dimen/im_dp_10"
android:padding="@dimen/im_dp_10"
android:text="@string/photograph"
android:textColor="@color/color_333333"
android:textSize="@dimen/txt_size_32px" />
</RelativeLayout>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppThemeNoActionBar" parent="FastdevelopAppTheme">
<item name="android:windowBackground">@color/background_quanju</item>
</style>
</resources>
\ No newline at end of file
...@@ -12,4 +12,7 @@ ...@@ -12,4 +12,7 @@
<string name="downloading">正在下载</string> <string name="downloading">正在下载</string>
<string name="install_apk_error">安装APK错误</string> <string name="install_apk_error">安装APK错误</string>
<string name="go_browsers">跳转浏览器,下载安装APK</string> <string name="go_browsers">跳转浏览器,下载安装APK</string>
<string name="album">相册</string>
<string name="photograph">拍照</string>
</resources> </resources>
...@@ -90,4 +90,15 @@ ...@@ -90,4 +90,15 @@
<item name="android:textSize">@dimen/txt_size_28px</item> <item name="android:textSize">@dimen/txt_size_28px</item>
</style> </style>
<style name="AppThemeNoActionBar" parent="FastdevelopAppTheme">
<item name="android:windowBackground">@color/white</item>
</style>
<style name="FastdevelopAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<!--去除滑动时 顶部和底部的阴影-->
<item name="android:overScrollMode">never</item>
</style>
</resources> </resources>
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/com.mayi.fastdevelop/" name="files_root" />
<external-path path="." name="external_storage_root" />
<files-path name="download" path="." />
<external-path name="external_storage_download" path="Download"/>
</paths>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment