【原】android 常用API
1、handleOnWindowFocusChanged(boolean hasFocus) 监控滑动通知条事件。
public static native void handleOnWindowFocusChanged(boolean hasFocus);
@Override
public void onWindowFocusChanged(final boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.e("aAAAAAAAAAAAAAA","AAAAAAAAAAAAAAAAA::"+hasFocus);
}
}
2、android 保存图片到sd卡
File file = new File("/sdcard/myImage/");
file.mkdirs();// 创建文件夹
String fileName = "/sdcard/myImage/aa.jpg";
FileOutputStream outStream = new FileOutputStream(fileName);
//写入数据
outStream.write(b); //b 图片数据流
outStream.flush();
//关闭输出流
outStream.close();
3、sd卡读取图片
Bitmap bitmap = null;
try
{
File file = new File("/sdcard/myImage/data.png");
Log.e("SSSSSSSS", " "+file.exists());
if(file.exists())
{
bitmap = BitmapFactory.decodeFile("/sdcard/myImage/data.png");
Log.e("AAAAA","AAAAAAAAAAAAAAA");
}
else
{
Log.e("AAAAA","BBBBBBBBBBBBBBBB");
}
} catch (Exception e)
{
// TODO: handle exception
}
bitmap 转byte[]
public byte[] Bitmap2Bytes(Bitmap bm) {
Log.e("AAAAA",":SSSSSSSSSSSSS::"+bm);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
Log.e("AAAAA",":::"+ baos.toByteArray().length);
return baos.toByteArray();
}
4、获取系统时间
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);
5、获取sd卡路径
public String getSDPath(){
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist)
{
sdDir = Environment.getExternalStorageDirectory();//获取跟目录
}
return sdDir.toString();
}
6、获取相册图片路径:
(1)打开相册:
Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
getAlbum.setType(IMAGE_TYPE);
startActivityForResult(getAlbum, IMAGE_CODE);
(2)选择图片:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode != RESULT_OK) { //此处的 RESULT_OK 是系统自定义得一个常量
// Log.e(TAG,"ActivityResult resultCode error");
return;
}
Bitmap bm = null;
ContentResolver resolver = getContentResolver();
if (requestCode == IMAGE_CODE) {
try {
Uri originalUri = data.getData(); //获得图片的uri
bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); //显得到bitmap图片
// 这里开始的第二部分,获取图片的路径:
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(originalUri, proj, null, null, null);
//按我个人理解 这个是获得用户选择的图片的索引值
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//最后根据索引值获取图片路径
String path = cursor.getString(column_index);
Log.e("Lostinai",path);
}catch (IOException e) {
Log.e("Lostinai",e.toString());
}
}
}