星云 Android 开发工具箱
Toast 工具类:
SmartToastUtils.java
import android.content.Context;
import android.widget.Toast; /**
* Toast 弹出信息工具类,简化代码编写
* @author fairy
* */
public class SmartToastUtils {
public static void showLong(Context context, String info) {
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
public static void showShort(Context context,String info) {
Toast.makeText(context, info,Toast.LENGTH_SHORT).show();
}
}
打印日志工具类:
SmartLogUtils.java
import android.util.Log;
public class SmartLogUtils {
private final static String DEBUG_TAG="xingyun";
/***
* 封装日志打印方法
* @param message 打印的消息
* @param showMessage 是否显示打印的消息
* **/
public static void showInfo(String message,Boolean showMessage){
if(showMessage){
int max_str_length = 2001 - DEBUG_TAG.length();
//大于4000时
while (message.length() > max_str_length) {
Log.i(DEBUG_TAG, message.substring(0, max_str_length));
message = message.substring(max_str_length);
}
//剩余部分
Log.i(DEBUG_TAG,message);
}
}
/***
* 封装日志打印方法
* @param message 打印的消息
* @param showMessage 是否显示打印的消息
* **/
public static void showDebug(String message,Boolean showMessage){
if(showMessage){
int max_str_length = 2001 - DEBUG_TAG.length();
//大于4000时
while (message.length() > max_str_length) {
Log.d(DEBUG_TAG, message.substring(0, max_str_length));
message = message.substring(max_str_length);
}
//剩余部分
Log.d(DEBUG_TAG,message);
}
}
public static void showError(String message,Boolean showMessage){
if(showMessage){
int max_str_length = 2001 - DEBUG_TAG.length();
//大于4000时
while (message.length() > max_str_length) {
Log.e(DEBUG_TAG, message.substring(0, max_str_length));
message = message.substring(max_str_length);
}
//剩余部分
Log.e(DEBUG_TAG,message);
}
}
}
Uri 转BitMap工具类:
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor =
getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return image;
}
打开系统内容提供器获取所有图片:
//打开搜索到的所有设备图片页面
private static final int READ_REQUEST_CODE = 42;
public void performFileSearch() { // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
// browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); // Filter to only show results that can be "opened", such as a
// file (as opposed to a list of contacts or timezones)
intent.addCategory(Intent.CATEGORY_OPENABLE); // Filter to show only images, using the image MIME data type.
// If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
// To search for all documents available via installed storage providers,
// it would be "*/*".
intent.setType("image/*");
startActivityForResult(intent, READ_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) { // The ACTION_OPEN_DOCUMENT intent was sent with the request code
// READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
// response to some other intent, and the code below shouldn't run at all. if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter.
// Pull that URI using resultData.getData().
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
SmartToastUtils.showShort(MainActivity.this, "URI=" + uri.toString());
showURITextView.setText(uri.toString());
dumpImageMetaData(uri);
try {
saveBitMapToPhoneDevice(getBitmapFromUri(uri));
checkedPicFileImageView.setImageURI(uri);
} catch (IOException e) {
SmartToastUtils.showShort(MainActivity.this, "图片解析失败" + e.toString());
}
}
}
} //将BitMap保存到手机上
private void saveBitMapToPhoneDevice(Bitmap bitmap) {
//将保存到SDCard /xingyun_test_create 文件夹下
File appDir = new File(Environment.getExternalStorageDirectory(), "xingyun_test_create");
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + "AAA.jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
SmartToastUtils.showShort(MainActivity.this,"save success");
}
Uri 解析图片信息
//解析URI 读取图片信息 实际大小和实际名称
public void dumpImageMetaData(Uri uri) { // The query, since it only applies to a single document, will only return
// one row. There's no need to filter, sort, or select fields, since we want
// all fields for one document.
Cursor cursor = MainActivity.this.getContentResolver()
.query(uri, null, null, null, null, null); try {
// moveToFirst() returns false if the cursor has 0 rows. Very handy for
// "if there's anything to look at, look at it" conditionals.
if (cursor != null && cursor.moveToFirst()) { // Note it's called "Display Name". This is
// provider-specific, and might not necessarily be the file name.
String displayName = cursor.getString(
cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
SmartToastUtils.showShort(MainActivity.this, "Display Name: " + displayName); int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
// If the size is unknown, the value stored is null. But since an
// int can't be null in Java, the behavior is implementation-specific,
// which is just a fancy term for "unpredictable". So as
// a rule, check if it's null before assigning to an int. This will
// happen often: The storage API allows for remote files, whose
// size might not be locally known.
String size = null;
if (!cursor.isNull(sizeIndex)) {
// Technically the column stores an int, but cursor.getString()
// will do the conversion automatically.
size = cursor.getString(sizeIndex);
} else {
size = "Unknown";
}
// Log.i(TAG, "Size: " + size);
SmartToastUtils.showShort(MainActivity.this, "Size=" + size);
}
} finally {
cursor.close();
}
}
网络状态监听广播类:
public final static String LISTENING_NETWORK_STATUS="android.net.conn.CONNECTIVITY_CHANGE";
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo; import com.xingyun.smartusbdeviceapp.util.SmartToastUtils; /*******
* 网络状态监听改变需要这个权限
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
* */
public class NetworkChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//根据系统服务类 获取管理网络连接
ConnectivityManager connectivityManager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获取网络连接信息
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
//检查网络是否连接成功
if(networkInfo!=null && networkInfo.isConnected()){
SmartToastUtils.showShort(context,"network is connected");
}else{
SmartToastUtils.showShort(context,"network is disconnected");
}
//拦截掉广播
//abortBroadcast();
}
}
文件工具类:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList; /**
* Created by apple on 2018/7/13.
*/ public class FileUtils {
/**
* 把字节数组保存为一个文件
* @param
*/
public static File bytes2File(byte[] b, String outputFile) {
BufferedOutputStream stream = null;
File file = null;
try {
file = new File(outputFile);
if(!file.getParentFile().exists()){
boolean mkdirs = file.getParentFile().mkdirs();
}
boolean newFile = file.createNewFile();
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return file;
} // 用于遍历sdcard卡上所有文件的类
public static void DirAll(File dirFile) throws Exception {
// 用于存放sdcard卡上的所有图片路径
ArrayList<String> dirAllStrArr = new ArrayList<String>();
if (dirFile.exists()) {
File files[] = dirFile.listFiles();
for (File file : files) {
if (file.isDirectory()) {
String fileName = file.getName();
// 除sdcard上Android这个文件夹以外。
if (!fileName.endsWith("Android")) {
// 如果遇到文件夹则递归调用。
DirAll(file);
}
} else {
// 如果是图片文件压入数组
String fileName = file.getName();
if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")
|| fileName.endsWith(".bmp")
|| fileName.endsWith(".gif")
|| fileName.endsWith(".png")) {
// 如果遇到文件则放入数组
if (dirFile.getPath().endsWith(File.separator)) {
dirAllStrArr
.add(dirFile.getPath() + file.getName());
} else {
dirAllStrArr.add(dirFile.getPath() + File.separator + file.getName());
}
}
}
}
}
}
}
public class NetworkReceiverConstant {
public final static String LISTENING_NETWORK_STATUS="android.net.conn.CONNECTIVITY_CHANGE";
}
星云 Android 开发工具箱的更多相关文章
- Python 帮你玩微信跳一跳 GitHub Python脚本
前言想自己搞游戏小程序的 在github 有人已经利用 python程序, 通过adb 获取不同型号安卓手机的系统截图,然后通过计算小人与目标位置距离之后得到准确的触摸时间,再通过 开发者模式里的 a ...
- 星云測试- Android应用深度体检专业平台
星云測试-给你的Android应用做个深度体检 星云測试- Android应用深度体检专业平台 星云在线云測试(简称星云測试www.teststars.cc)是全球第一个公布并商用的数字化精准软件 ...
- Android Studio 星云常用配置工具箱
1. 安装插件 1.1 Android View绑定框架 开源地址:https://github.com/JakeWharton/butterknife 插件地址: https://github.co ...
- 五步搞定Android开发环境部署
引言 在windows安装Android的开发环境不简单也说不上算复杂,本文写给第一次想在自己Windows上建立Android开发环境投入 Android浪潮的朋友们,为了确保大家能顺利完成开发 ...
- 安卓学习进程(2)Android开发环境的搭建
本节将分为五个步骤来完成Android开发环境的部署. 第一步:安装JDK. 第二步:配置Windows上JDK的变量环境 . 第三步:下载安装Eclipse . 第四步:下载安装Androi ...
- android开发环境搭建日记和嵌入式Android开发环境初探
非常感谢博客园的各位,按照你们的博文,还有利用百度和谷歌逐渐建立了android的开发环境,只是给自己备份参考查看,看过的人可以忽略这篇文章. 本文章大部分参考了:http://www.cnblogs ...
- Android开发环境部署
引言 在windows系统中安装Android的开发环境,将分为五个步骤来完成: 第一步:安装JDK 第二步:配置Windows上JDK的变量环境 第三步: 下载安装Eclipse 第四步:下载安 ...
- 五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程
在windows安装Android的开发环境不简单也说不上算复杂,本文写给第一次想在自己Windows上建立Android开发环境投入Android浪潮的朋友们,为了确保大家能顺利完成开发环境的搭 ...
- Android开发新手学习总结(一)——使用Android Studio搭建Android集成开发环境
[新手连载]一:使用Android Studio搭建Android集成开发环境http://bbs.itcast.cn/forum.php?mod=viewthread&tid=87055&a ...
随机推荐
- android ndk native错误分析方法
使用ndk自带的工具进行分析, /mnt/d/Projects/linuxEnv/env/toolchains/aarch64-linux-android-4.9/bin/aarch64-linux- ...
- Flash和js交互的效率分析
Flash和js交互的效率分析 AS代码: var time:int = getTimer(); for (var i:int = 0; i < 50000; i++) { External ...
- TL Cop
第一讲 研发技术型人才的特点 一.为何要重视技术与研发技术管理 1.中国技术型企业近十年的发展路线图以及经验教训 2.技术还是营销:中国企业的痛 3.名家论技术与研发技术管理 二.研发技术人才的特点 ...
- ajax, jQuery, jQueryeasyUI
1.ajax与jQueryajax是jquery库里面的一个被封装好的函数,可以拿来直接使用.没有jquery的话,ajax的使用就得用原生的javascript去写,比较麻烦. 2.jQuery E ...
- MATLAB 的 cell 大法(单元格数组)
MATLAB 的 cell,称单元格数组 or 元胞数组:使用频率特别高,甚至比 struct 结构体还高. MATLAB文档给出的 cell 官方定义: A cell array is a coll ...
- linux shell 脚本攻略学习15--如何只列出目录,如何快速切换目录
工作中经常遇到关于目录方面的问题,例如,如何只列出当前目录下的所有目录,以及如何快速高效的切换目录,而不需要使用鼠标,下面将简单介绍关于这两方面的解决方案: 一.如何只列出目录? 看似简单的任务,其实 ...
- JavaScript Window Navigator 浏览器本身的信息
window.navigator 对象包含有关访问者浏览器的信息. Window Navigator window.navigator 对象在编写时可不使用 window 这个前缀. Navigato ...
- cd及目录快速切换
一.cd ~ 切换到用户目录 二.cd - cd - 返回进入当前目录前所在目录 三.pushd.popd.dirs 在Linux的多目录命令提示符中工作是一种痛苦的事情,但以下这些利用lin ...
- 搞不懂为什么开发人员爱iOS恨Android?
导读:很多网站发表文章大同小异.唯有这个不同点,给大家分享. Android和iOS的较量一直都是人们津津乐道的话题.两个平台各有各的优势所在,同时也都力图能在各个方面赶超对手.对于用户来说,青菜萝卜 ...
- 根据友盟统计错误分析线上的崩溃-b
登陆友盟官网找到友盟统计,找到你iOS平台下你所属的APP(图1) 图1 点击进去会出现当日错误列表,选择你发生错误的日期(图2) 图2 我们可以看到,这一天中出现了两个错误,每个错误出现在不同的时间 ...