星云 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 ...
随机推荐
- PHP中使用ActiveMQ实现消息队列
前面我们已经学了怎样部署ActiveMQ. 我们知道通过ActiveMQ的一个管理后台能够查看任务队列. 今天 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- 检测动态生成的单选按钮和jQuery的变化
我试图发现变化动态生成的单选按钮.我有一个生成的单选按钮的表单,他们根据自己的ID保存在数据库.所以这是什么 <input type="radio" name="d ...
- vs code 问题:preLaunchTask“build”已终止,退出代码为 1。解决办法
菜单:任务-配置任务 改为如下: { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation ab ...
- phpBB3.1和3.2
闲逛看到一个帖子, phpBB3的论坛经常被机器人注册攻击, 要找人做拦截. 出于兴趣, 联系了一下. 论坛是一个做健康养老调查的社会机构的, 版本3.1, 十年前我还在更新phpbb中文包的时候, ...
- 跟我学Shiro---无状态 Web 应用集成
无状态 Web 应用集成 在一些环境中,可能需要把 Web 应用做成无状态的,即服务器端无状态,就是说服务器端不会存储像会话这种东西,而是每次请求时带上相应的用户名进行登录.如一些 REST 风格的 ...
- open()系统调用的实现
open系统调用的服务例程是sys_open()函数,它接受三个参数:要打开文件的路径名filename, 访问模式的表示flags和文件权限掩码mode.在内核中,sys_open实际调用do_sy ...
- 时间选择器(js,css,html)
忘了从哪下载的了, 整理电脑, 做个记录, 效果图: 下载链接: 链接: https://pan.baidu.com/s/1qfYkcfn8dtLFg0oniB2GHA 提取码: 2amm
- spring加载jar包中多个配置文件(转)
转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <co ...
- 【RS】Matrix Factorization Techniques for Recommender Systems - 推荐系统的矩阵分解技术
[论文标题]Matrix Factorization Techniques for Recommender Systems(2009,Published by the IEEE Computer So ...
- ipsec在企业网中的应用(IKE野蛮模式)(转)
from:http://lulu1101.blog.51cto.com/4455468/817954 ipsec在企业网中的应用(IKE野蛮模式) 案例: 本实验采用华为三台F100防火墙,和一台s3 ...