package com.tejia.lijin.app.util;
|
|
import android.content.ComponentName;
|
import android.content.ContentValues;
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.content.pm.ApplicationInfo;
|
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageManager;
|
import android.database.Cursor;
|
import android.graphics.Bitmap;
|
import android.graphics.BitmapFactory;
|
import android.media.MediaScannerConnection;
|
import android.net.Uri;
|
import android.os.Build;
|
import android.os.Environment;
|
import android.provider.MediaStore;
|
import android.util.Log;
|
import android.widget.Toast;
|
|
import com.wpc.lcjianlibrary.BuildConfig;
|
import com.wpc.library.util.MobileUtil;
|
import com.tejia.lijin.app.ui.dialog.UnbindDialog;
|
import com.wpc.library.util.common.DeviceUtil;
|
|
import java.io.File;
|
import java.io.FileNotFoundException;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.List;
|
|
|
/**
|
* Created by Administrator on 2017/11/15.
|
*/
|
public class Tools {
|
|
public static String IMAGE_NAME = "iv_share_";
|
public static int i = 0;
|
|
//保存网络图片保存到本地
|
public static final File saveImageToSdCard(Context context, String image) {
|
boolean success = false;
|
File file = null;
|
try {
|
file = createStableImageFile(context);
|
Bitmap bitmap = null;
|
URL url = new URL(image);
|
HttpURLConnection conn = null;
|
conn = (HttpURLConnection) url.openConnection();
|
String length = conn.getHeaderField("Content-Length");
|
// Log.e("mResult", length);
|
InputStream is = null;
|
is = conn.getInputStream();
|
bitmap = BitmapFactory.decodeStream(is);
|
int size = bitmap.getByteCount();
|
// Log.e("mResult", size + "");
|
FileOutputStream outStream;
|
|
outStream = new FileOutputStream(file);
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
|
outStream.flush();
|
outStream.close();
|
success = true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
if (success) {
|
return file;
|
} else {
|
return null;
|
}
|
}
|
|
//保存网络图片保存到本地
|
public static final File saveImageFromPathToSdCard(Context context, String image,
|
String path, String imageFileName) {
|
boolean success = false;
|
File file = null;
|
try {
|
file = createImageFilePath(path, imageFileName);
|
Bitmap bitmap = null;
|
URL url = new URL(image);
|
HttpURLConnection conn = null;
|
conn = (HttpURLConnection) url.openConnection();
|
InputStream is = null;
|
is = conn.getInputStream();
|
bitmap = BitmapFactory.decodeStream(is);
|
FileOutputStream outStream;
|
|
outStream = new FileOutputStream(file);
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
|
outStream.flush();
|
outStream.close();
|
success = true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
if (success) {
|
MediaScannerConnection.scanFile(context, new String[]{file.getPath()}, null, null);
|
return file;
|
} else {
|
return null;
|
}
|
}
|
|
//保存bitmap图片
|
public static File saveBitmap(Context mContext, Bitmap bitmap) {
|
String imageFileName = System.currentTimeMillis() + ".jpg";
|
String path = Environment.getExternalStorageDirectory()
|
+ "/rebateshare";
|
File storageDir = new File(path);
|
if (!storageDir.exists()) {
|
storageDir.mkdir();
|
}
|
|
// Toast.makeText(this, "宽:" + bitmap.getWidth() + "---高:" + bitmap.getHeight(), Toast.LENGTH_LONG).show();
|
// Log.e("mResult", "bitmap宽:" + bitmap.getWidth() + "---bitmap高:" + bitmap.getHeight());
|
// 保存图片到SD卡上
|
File imgfile = new File(path, imageFileName);
|
FileOutputStream stream;
|
try {
|
stream = new FileOutputStream(imgfile);
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
|
// 把文件插入到系统图库(注释是因为 会在相册和保存文件夹内各有一张图片)
|
// try {
|
// MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
|
// imgfile.getAbsolutePath(), imageFileName, null);
|
// } catch (FileNotFoundException e) {
|
// e.printStackTrace();
|
// }
|
//通知系统消息更新
|
MediaScannerConnection.scanFile(mContext, new String[]{imgfile.toString()}, null, null);
|
|
// Android设备Gallery应用只会在启动的时候扫描系统文件夹
|
// 这里模拟一个媒体装载的广播,用于使保存的图片可以在Gallery中查看
|
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
// Intent mediaScanIntent = new Intent(
|
// Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
|
// Uri contentUri = Uri.fromFile(Environment
|
// .getExternalStorageDirectory()); // out is your output
|
// // file
|
// mediaScanIntent.setData(contentUri);
|
// mContext.sendBroadcast(mediaScanIntent);
|
// } else {
|
// Intent intent = new Intent();
|
// intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
|
// intent.setData(Uri.fromFile(Environment
|
// .getExternalStorageDirectory()));
|
// mContext.sendBroadcast(intent);
|
// }
|
} catch (FileNotFoundException e) {
|
Log.i("mResult", "文件没有找到");
|
e.printStackTrace();
|
}
|
return imgfile;
|
}
|
|
|
//保存bitmap图片
|
public static File saveBitmap(Bitmap bitmap, String path, String name) {
|
String imageFileName = name;
|
File storageDir = new File(path);
|
if (!storageDir.exists()) {
|
storageDir.mkdirs();
|
}
|
|
// Toast.makeText(this, "宽:" + bitmap.getWidth() + "---高:" + bitmap.getHeight(), Toast.LENGTH_LONG).show();
|
// Log.e("mResult", "bitmap宽:" + bitmap.getWidth() + "---bitmap高:" + bitmap.getHeight());
|
// 保存图片到SD卡上
|
File imgfile = new File(path, imageFileName);
|
FileOutputStream stream;
|
try {
|
stream = new FileOutputStream(imgfile);
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
|
} catch (FileNotFoundException e) {
|
Log.i("mResult", "文件没有找到");
|
e.printStackTrace();
|
}
|
return imgfile;
|
}
|
|
|
//保存bitmap图片
|
public static File saveBitmapToPath(Context mContext, Bitmap bitmap, String path, String imageFileName, String ToastName) {
|
File storageDir = new File(path);
|
if (!storageDir.exists()) {
|
storageDir.mkdir();
|
}
|
// 保存图片到SD卡上
|
File imgfile = new File(path, imageFileName);
|
FileOutputStream stream;
|
try {
|
stream = new FileOutputStream(imgfile);
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
|
// 其次把文件插入到系统图库(注释是因为 会在相册和保存文件夹内各有一张图片)
|
// try {
|
// MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
|
// imgfile.getAbsolutePath(), imageFileName, null);
|
// } catch (FileNotFoundException e) {
|
// e.printStackTrace();
|
// }
|
//通知系统消息更新
|
MediaScannerConnection.scanFile(mContext, new String[]{imgfile.toString()}, null, null);
|
// "分享图保存成功,请到相册中查看"
|
Toast.makeText(mContext, ToastName, Toast.LENGTH_LONG).show();
|
} catch (FileNotFoundException e) {
|
Log.i("mResult", "文件没有找到");
|
Toast.makeText(mContext, "图片保存失败", Toast.LENGTH_LONG).show();
|
e.printStackTrace();
|
}
|
return imgfile;
|
}
|
|
public static void saveImageToGallery(Context context, Bitmap bmp, String path, String imageFileName) {
|
// 首先保存图片
|
File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
|
if (!appDir.exists()) {
|
appDir.mkdir();
|
}
|
File file = new File(appDir, imageFileName);
|
try {
|
FileOutputStream fos = new FileOutputStream(file);
|
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
|
fos.flush();
|
fos.close();
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
// 其次把文件插入到系统图库
|
try {
|
MediaStore.Images.Media.insertImage(context.getContentResolver(),
|
file.getAbsolutePath(), imageFileName, null);
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
}
|
// 最后通知图库更新
|
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
|
}
|
|
//创建本地保存路径
|
public static File createStableImageFile(Context context) throws IOException {
|
i++;
|
String imageFileName = IMAGE_NAME + i + ".png";
|
String path = context.getExternalCacheDir().getPath() + "/share";
|
File storageDir = new File(path);
|
if (!storageDir.exists()) {
|
storageDir.mkdir();
|
}
|
File image = new File(storageDir, imageFileName);
|
return image;
|
}
|
|
//创建本地保存路径
|
public static File createImageFilePath(String path, String imageFileName) throws IOException {
|
File storageDir = new File(path);
|
if (!storageDir.exists()) {
|
storageDir.mkdir();
|
}
|
File image = new File(storageDir, imageFileName);
|
return image;
|
}
|
|
// 判断文件是否存在
|
public static void singleFileExists(File file) {
|
|
if (file.exists()) {
|
System.out.println("file exists");
|
} else {
|
System.out.println("file not exists, create it ...");
|
try {
|
file.createNewFile();
|
} catch (IOException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
}
|
|
}
|
|
// 判断文件夹是否存在
|
public static void fileDirExists(File file) {
|
if (file.exists()) {
|
if (file.isDirectory()) {
|
System.out.println("dir exists");
|
} else {
|
System.out.println("the same name file exists, can not create dir");
|
}
|
} else {
|
System.out.println("dir not exists, create it ...");
|
file.mkdir();
|
}
|
|
}
|
|
|
//判断是否安装了淘宝
|
public static int isTaobaoAvilible(final Context context) {
|
int returnNum;
|
final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
|
List<PackageInfo> pinfo = DeviceUtil.getInstalledPackagesCache(context);
|
if (pinfo != null) {
|
boolean isReadSuc = false;
|
for (int i = 0; i < pinfo.size(); i++) {
|
String pn = pinfo.get(i).packageName;
|
if (pn.equals(context.getPackageName())) {
|
isReadSuc = true;
|
}
|
}
|
if (isReadSuc) {
|
boolean isHave = false;
|
for (int i = 0; i < pinfo.size(); i++) {
|
String pn = pinfo.get(i).packageName;
|
if (pn.equals("com.taobao.taobao")) {
|
isHave = true;
|
}
|
}
|
if (isHave) {
|
returnNum = 0;
|
} else {
|
returnNum = 2;
|
}
|
} else {
|
returnNum = 1;
|
}
|
} else {
|
returnNum = 1;
|
}
|
if (returnNum == 1) {
|
openAppListPermission(context, "淘宝");
|
}
|
return returnNum;
|
}
|
|
/**
|
* @param context
|
* @return 0-安装了微信 1-没有读取应用权限 2-没有安装微信
|
*/
|
//判断是否安装了微信
|
public static int isWeixinAvilible(final Context context) {
|
int returnNum = 2;
|
final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
|
List<PackageInfo> pinfo = DeviceUtil.getInstalledPackagesCache(context);
|
if (pinfo != null) {
|
boolean isReadSuc = false;
|
for (int i = 0; i < pinfo.size(); i++) {
|
String pn = pinfo.get(i).packageName;
|
if (pn.equals(context.getPackageName())) {
|
isReadSuc = true;
|
}
|
}
|
if (isReadSuc) {
|
boolean isHave = false;
|
for (int i = 0; i < pinfo.size(); i++) {
|
String pn = pinfo.get(i).packageName;
|
if (pn.equals("com.tencent.mm")) {
|
isHave = true;
|
}
|
}
|
if (isHave) {
|
returnNum = 0;
|
} else {
|
returnNum = 2;
|
}
|
} else {
|
returnNum = 1;
|
}
|
} else {
|
returnNum = 1;
|
}
|
if (returnNum == 1) {
|
openAppListPermission(context, "微信");
|
}
|
return returnNum;
|
}
|
|
//判断是否安装了sina
|
public static int isSinaAvilible(Context context) {
|
final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
|
List<PackageInfo> pinfo = DeviceUtil.getInstalledPackagesCache(context);
|
int returnNum = 2;
|
if (pinfo != null) {
|
boolean isReadSuc = false;
|
for (int i = 0; i < pinfo.size(); i++) {
|
String pn = pinfo.get(i).packageName;
|
if (pn.equals(context.getPackageName())) {
|
isReadSuc = true;
|
}
|
}
|
if (isReadSuc) {
|
boolean isHave = false;
|
for (int i = 0; i < pinfo.size(); i++) {
|
String pn = pinfo.get(i).packageName;
|
if (pn.equals("com.sina.weibo")) {
|
isHave = true;
|
}
|
}
|
if (isHave) {
|
returnNum = 0;
|
} else {
|
returnNum = 2;
|
}
|
} else {
|
returnNum = 1;
|
}
|
} else {
|
returnNum = 1;
|
}
|
if (returnNum == 1) {
|
openAppListPermission(context, "新浪");
|
}
|
return returnNum;
|
}
|
|
//判断是否安装了QQ
|
public static int isQQAvilible(Context context) {
|
final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
|
List<PackageInfo> pinfo = DeviceUtil.getInstalledPackagesCache(context);
|
int returnNum;
|
if (pinfo != null) {
|
boolean isReadSuc = false;
|
for (int i = 0; i < pinfo.size(); i++) {
|
String pn = pinfo.get(i).packageName;
|
if (pn.equals(context.getPackageName())) {
|
isReadSuc = true;
|
}
|
}
|
if (isReadSuc) {
|
boolean isHave = false;
|
for (int i = 0; i < pinfo.size(); i++) {
|
String pn = pinfo.get(i).packageName;
|
if (pn.equals("com.tencent.mobileqq")) {
|
isHave = true;
|
}
|
}
|
if (isHave) {
|
returnNum = 0;
|
} else {
|
returnNum = 2;
|
}
|
} else {
|
returnNum = 1;
|
}
|
} else {
|
returnNum = 1;
|
}
|
if (returnNum == 1) {
|
openAppListPermission(context, "QQ");
|
}
|
return returnNum;
|
}
|
|
private static void openAppListPermission(final Context context, String platform) {
|
UnbindDialog.Builder builder = new UnbindDialog.Builder(context);
|
builder.setMessage("需要检测是否安装" + platform + ",请打开 读取应用列表权限")
|
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
dialogInterface.dismiss();
|
}
|
}).setPositiveButton("确认", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
dialogInterface.dismiss();
|
if (MobileUtil.getSystem().equals("MIUI")) {
|
gotoMiuiPermission(context);
|
} else if (MobileUtil.getSystem().equals("EMUI")) {
|
gotoHuaweiPermission(context);
|
} else if (MobileUtil.getSystem().equals("flyme")) {
|
gotoMiuiPermission(context);
|
} else {
|
context.startActivity(getAppDetailSettingIntent(context));
|
}
|
}
|
}).create().show();
|
}
|
|
/**
|
* 跳转到miui的权限管理页面
|
*/
|
public static void gotoMiuiPermission(Context context) {
|
Intent i = new Intent("miui.intent.action.APP_PERM_EDITOR");
|
ComponentName componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
|
i.setComponent(componentName);
|
i.putExtra("extra_pkgname", context.getPackageName());
|
try {
|
context.startActivity(i);
|
} catch (Exception e) {
|
e.printStackTrace();
|
context.startActivity(getAppDetailSettingIntent(context));
|
}
|
}
|
|
/**
|
* 跳转到魅族的权限管理系统
|
*/
|
private static void gotoMeizuPermission(Context context) {
|
Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
|
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
intent.putExtra("packageName", context.getPackageName());
|
try {
|
context.startActivity(intent);
|
} catch (Exception e) {
|
e.printStackTrace();
|
context.startActivity(getAppDetailSettingIntent(context));
|
}
|
}
|
|
/**
|
* 华为的权限管理页面
|
*/
|
private static void gotoHuaweiPermission(Context context) {
|
try {
|
Intent intent = new Intent();
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
|
intent.setComponent(comp);
|
context.startActivity(intent);
|
} catch (Exception e) {
|
e.printStackTrace();
|
context.startActivity(getAppDetailSettingIntent(context));
|
}
|
}
|
|
/**
|
* 获取应用详情页面intent
|
*
|
* @return
|
*/
|
private static Intent getAppDetailSettingIntent(Context context) {
|
Intent localIntent = new Intent();
|
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
if (Build.VERSION.SDK_INT >= 9) {
|
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
|
localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
|
} else if (Build.VERSION.SDK_INT <= 8) {
|
localIntent.setAction(Intent.ACTION_VIEW);
|
localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
|
localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
|
}
|
return localIntent;
|
}
|
|
/**
|
* @param context
|
* @param imageFile
|
* @return content Uri
|
*/
|
public static Uri getImageContentUri(Context context, File imageFile) {
|
// if (1 > 0) {
|
// try {
|
// ApplicationInfo applicationInfo = context.getApplicationInfo();
|
// int targetSDK = applicationInfo.targetSdkVersion;
|
// Uri uri;
|
// if (targetSDK >= Build.VERSION_CODES.N && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
// uri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(context.getContentResolver(),
|
// imageFile.getAbsolutePath(), imageFile.getName(), null));
|
// } else {
|
// uri = Uri.fromFile(new File(imageFile.getPath()));
|
// }
|
//
|
// return uri;
|
// } catch (Exception e) {
|
// return null;
|
// }
|
//
|
// }
|
|
|
String filePath = imageFile.getAbsolutePath();
|
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=? ",
|
new String[]{filePath}, null);
|
if (cursor != null && cursor.moveToFirst()) {
|
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
|
Uri baseUri = Uri.parse("content://media/external/images/media");
|
return Uri.withAppendedPath(baseUri, "" + id);
|
} else {
|
if (imageFile.exists()) {
|
ContentValues values = new ContentValues();
|
values.put(MediaStore.Images.Media.DATA, filePath);
|
return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
|
} else {
|
return null;
|
}
|
}
|
}
|
}
|