判断Android的版本号是否在6.0之上
if(Build.VERSION.SDK_INT >= 23) {
//Android6.0以上,需要动态申请运行时权限
}
else{
//Android6.0以下,直接进行地图定位操作
}
给Button添加图片
private void setViewDrawable(int drawableid, TextView view, int driection) {
Drawable drawable = getResources().getDrawable(drawableid);
drawable.setBounds(0, 0, 100, 100);
Drawable[] drawables = new Drawable[4];
drawables[driection] = drawable;
view.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
view.setCompoundDrawablePadding(0);
}
setViewDrawable(R.drawable.addphoto, addphoto2, 1);
使用日期选择器
public static void showDatePickerDialog(Activity activity, final Button button, Calendar calendar) {
// Calendar 需要这样来得到
// Calendar calendar = Calendar.getInstance();
// 直接创建一个DatePickerDialog对话框实例,并将它显示出来
new DatePickerDialog(activity,
// 绑定监听器(How the parent is notified that the date is set.)
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// 此处得到选择的时间,可以进行你想要的操作
button.setText( + year + "年" + monthOfYear
+ "月" + dayOfMonth + "日");
}
}
// 设置初始日期
, calendar.get(Calendar.YEAR)
,calendar.get(Calendar.MONTH)
,calendar.get(Calendar.DAY_OF_MONTH)).show();
}
使用,这次用的是碎片,传入了点击的Button然后把日期在显示在Button上
Calendar calendar = Calendar.getInstance();
I_pick_thing activity=(I_pick_thing)getActivity();
showDatePickerDialog(getActivity(),activity.getFind_time(),calendar);
Android : Resource is not a Drawable (color or path)问题
将图片从drawable 拷贝到 drawable-v24保证两边都有,就行了。
Bmob上传单张图片
lostItem.setName(mName);
lostItem.setLabel(mLabel);
lostItem.setFounder(mFounder);
lostItem.setTel(mTel);
lostItem.setQQ(mQQ);
lostItem.setWeChat(mWeChat);
lostItem.setPickedPlace(mPickedPlace);
lostItem.setPickedDate(mPickedDate);
lostItem.setUserAccount(mUserAccount);
lostItem.setOption(mOption);
//以上为其他信息
final BmobFile file = new BmobFile(photo1_File);//photo1_File为照片存储路径
file.uploadblock(new UploadFileListener() {
@Override
public void done(BmobException e) {
if (e == null) {
lostItem.setImageA(file);
lostItem.save(new SaveListener<String>() {
@Override
public void done(String s, BmobException e) {
if (e == null) {
Toast.makeText(I_pick_thing.this, "添加数据成功", Toast.LENGTH_SHORT).show();
finish();
} else {
//Toast.makeText(I_pick_thing.this, "创建数据失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("I_pick_thing", "添加数据失败" + e.getMessage());
}
}
});
}
}
});
Bmob批量上传多张图片
String filePath_mp3 = photo1_File.toString();//photo1_File为照片存储路径
String filePath_lrc = photo2_File.toString();//photo2_File为照片存储路径
final String[] filePaths = new String[2];
filePaths[0] = filePath_mp3;
filePaths[1] = filePath_lrc;
BmobFile.uploadBatch(filePaths, new UploadBatchListener() {
@Override
public void onSuccess(List<BmobFile> list, List<String> list1) {
if(list.size()==filePaths.length){//如果数量相等,则代表文件全部上传完成
//do something
lostItem.setImageA(list.get(0));
lostItem.setImageB(list.get(1));
lostItem.save(new SaveListener<String>() {
@Override
public void done(String s, BmobException e) {
if (e == null) {
Toast.makeText(I_pick_thing.this, "添加数据成功", Toast.LENGTH_SHORT).show();
finish();
} else {
//Toast.makeText(I_pick_thing.this, "创建数据失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("I_pick_thing", "添加数据失败" + e.getMessage());
}
}
});
}
}
@Override
public void onProgress(int i, int i1, int i2, int i3) {
//进度动画
ProgressDialog progressDialo = new ProgressDialog(I_pick_thing.this);
progressDialo.setTitle("信息正在上传");
progressDialo.setMessage("Loading...");
progressDialo.incrementProgressBy(i);
progressDialo.show();
}
@Override
public void onError(int i, String s) {
}
});
文件复制(亲测有效)
/**
* 复制单个文件
*
* @param oldPath$Name String 原文件路径+文件名 如:data/user/0/com.test/files/abc.txt
* @param newPath$Name String 复制后路径+文件名 如:data/user/0/com.test/cache/abc.txt
* @return <code>true</code> if and only if the file was copied;
* <code>false</code> otherwise
*/
public boolean copyFile(String oldPath$Name, String newPath$Name) {
try {
File oldFile = new File(oldPath$Name);
if (!oldFile.exists()) {
Log.e("--Method--", "copyFile: oldFile not exist.");
return false;
} else if (!oldFile.isFile()) {
Log.e("--Method--", "copyFile: oldFile not file.");
return false;
} else if (!oldFile.canRead()) {
Log.e("--Method--", "copyFile: oldFile cannot read.");
return false;
}
/* 如果不需要打log,可以使用下面的语句
if (!oldFile.exists() || !oldFile.isFile() || !oldFile.canRead()) {
return false;
}
*/
FileInputStream fileInputStream = new FileInputStream(oldPath$Name);
FileOutputStream fileOutputStream = new FileOutputStream(newPath$Name);
byte[] buffer = new byte[1024];
int byteRead;
while (-1 != (byteRead = fileInputStream.read(buffer))) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
缺陷:复制单文件时(copyFile),无法判断新文件夹是否存在、新文件是否能否写入。
复制文件夹内容(未测试)
/**
* 复制文件夹及其中的文件
*
* @param oldPath String 原文件夹路径 如:data/user/0/com.test/files
* @param newPath String 复制后的路径 如:data/user/0/com.test/cache
* @return <code>true</code> if and only if the directory and files were copied;
* <code>false</code> otherwise
*/
public boolean copyFolder(String oldPath, String newPath) {
try {
File newFile = new File(newPath);
if (!newFile.exists()) {
if (!newFile.mkdirs()) {
Log.e("--Method--", "copyFolder: cannot create directory.");
return false;
}
}
File oldFile = new File(oldPath);
String[] files = oldFile.list();
File temp;
for (String file : files) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file);
} else {
temp = new File(oldPath + File.separator + file);
}
if (temp.isDirectory()) { //如果是子文件夹
copyFolder(oldPath + "/" + file, newPath + "/" + file);
} else if (!temp.exists()) {
Log.e("--Method--", "copyFolder: oldFile not exist.");
return false;
} else if (!temp.isFile()) {
Log.e("--Method--", "copyFolder: oldFile not file.");
return false;
} else if (!temp.canRead()) {
Log.e("--Method--", "copyFolder: oldFile cannot read.");
return false;
} else {
FileInputStream fileInputStream = new FileInputStream(temp);
FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
byte[] buffer = new byte[1024];
int byteRead;
while ((byteRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}
/* 如果不需要打log,可以使用下面的语句
if (temp.isDirectory()) { //如果是子文件夹
copyFolder(oldPath + "/" + file, newPath + "/" + file);
} else if (temp.exists() && temp.isFile() && temp.canRead()) {
FileInputStream fileInputStream = new FileInputStream(temp);
FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
byte[] buffer = new byte[1024];
int byteRead;
while ((byteRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}
*/
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
设置软件开启时的动画展示
分割线
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:listDivider"//自带下划线颜色
/>
当前面有多个界面时,清除前面所有界面返回到主界面
比如MainActivity.java为程序的主界面
在manifest中设置MainActivity的启动模式为singleTask,在activity节点下加上如下:
android:launchMode="singleTask"
注意:singleTask模式的Activity不管是位于栈顶还是栈底,再次运行这个Activity时,都会destory掉它上面的Activity来保证整个栈中只有一个自己,切记切记”这点是毋庸置疑的。
比如你现在在 EActivity 与MainActivity之间有Activity B、C和D,当你需要回到MainAcitivity,并且需要销毁中间的界面时候就可以调用
startActivity(new Intent(EActivity.this,MainAcitivity.class));
销毁当前碎片
getActivity().onBackPressed();
android中bundle的使用
Bundle主要用于传递数据;它保存的数据,是以key-value(键值对)的形式存在的。
Bundle经常使用在Activity之间或者线程间传递数据,传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。
当Bundle传递的是对象或对象数组时,必须实现Serializable 或Parcelable接口。
Bundle提供了各种常用类型的putXxx()/getXxx()方法,用于读写基本类型的数据。(各种方法可以查看API)
在activity间传递信息
Bundle bundle = new Bundle(); //得到bundle对象
bundle.putString("sff", "value值"); //key-"sff",通过key得到value-"value值"(String型)
bundle.putInt("iff", 175); //key-"iff",value-175
intent.putExtras(bundle); //通过intent将bundle传到另个Activity
startActivity(intent);
读取数据
Bundle bundle = this.getIntent().getExtras(); //读取intent的数据给bundle对象
String str1 = bundle.getString("sff"); //通过key得到value
int int1 = bundle.getInt("iff");
线程间传递
通过Handler将带有dundle数据的message放入消息队列,其他线程就可以从队列中得到数据
Message message=new Message();//new一个Message对象
message.what = MESSAGE_WHAT_2;//给消息做标记
Bundle bundle = new Bundle(); //得到Bundle对象
bundle.putString("text1","消息传递参数的例子!"); //往Bundle中存放数据
bundle.putInt("text2",44); //往Bundle中put数据
message.setData(bundle);//mes利用Bundle传递数据
mHandler.sendMessage(message);//Handler将消息放入消息队列
读取数据
这里用的是Handler的handleMessage(Message msg)方法处理数据
String str1=msg.getData().getString("text1");
int int1=msg.getData().getString("text2");