android一些有用的方法,代码,和错误处理总结(持续更新)

在开发的过程中,难免有些问题重复出现,然后有略有些忘记,所以准备写这边一篇博客,持续更新,以便查阅:

 1.屏幕截图,把View转换成Bitmap

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 把一个View的对象转换成bitmap
*/
//如果你传的 view 是一个布局的 view 比如(LinearLayout) 就可以实现整个屏幕的截图了
static Bitmap getViewBitmap(View v) {
   v.clearFocus();
    v.setPressed( false );
//能画缓存就返回false
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing( false );
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor( 0 );
if (color != 0 ) {
     v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null ) {
     Log.e(TAG, "failed getViewBitmap(" + v + ")" , new
 
     RuntimeException());
     return null ;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}

//现在对 android 的 组件的 cache 机制讲解一下:
View组件显示的内容可以通过cache机制保存为bitmap, 使用到的api有

 

?
1
2
3
4
5
6
7
<span style= "color: #ff0000;" > void setDrawingCacheEnabled(boolean flag),</span>
 
<span style= "color: #ff0000;" >Bitmap getDrawingCache(boolean autoScale),</span>
 
<span style= "color: #ff0000;" > void buildDrawingCache(boolean autoScale),</span>
 
<span style= "color: #ff0000;" > void destroyDrawingCache()</span>


我们要获取它的cache先要通过setDrawingCacheEnable方法把cache开启,然后再调用getDrawingCache方法就可以获得view的cache图片了。buildDrawingCache方法可以不用调用,因为调用getDrawingCache方法时,若果cache没有建立,系统会自动调用buildDrawingCache方法生成cache。若果要更新cache, 必须要调用destoryDrawingCache方法把旧的cache销毁,才能建立新的。

当调用setDrawingCacheEnabled方法设置为false, 系统也会自动把原来的cache销毁。

ViewGroup在绘制子view时,而外提供了两个方法

void setChildrenDrawingCacheEnabled(boolean enabled)

setChildrenDrawnWithCacheEnabled(boolean enabled)

setChildrenDrawingCacheEnabled方法可以使viewgroup里所有的子view开启cache, setChildrenDrawnWithCacheEnabled使在绘制子view时,若该子view开启了cache, 则使用它的cache进行绘制,从而节省绘制时间。

获取cache通常会占用一定的内存,所以通常不需要的时候有必要对其进行清理,通过destroyDrawingCache或setDrawingCacheEnabled(false)实现。

2. Stream转换成Byte

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Stream转换成Byte
static byte[] streamToBytes(InputStream is ) {
ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
byte[] buffer = new byte[ 1024 ];
int len;
try {
    while ((len = is .read(buffer)) >= 0 ) {
    os.write(buffer, 0 , len);
}
} catch (java.io.IOException e) {
 
}
    return os.toByteArray();
}

3.读取raw资源文件中的mp3文件,然后通过音乐播放器播放:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
读取raw资源文件中的mp3文件,然后通过音乐播放器播放:
 
/**
* 把mp3文件写入卡
*
* @param fileName
* 输出的文件名(全路径)
* @param context
* context对象
*/
private void writeMP3ToSDcard( String fileName, Context context) {
byte[] buffer = new byte[ 1024 * 8 ];
int read;
BufferedInputStream bin = new BufferedInputStream(context.getResources().openRawResource
 
(R.raw.ring));
try {
   BufferedOutputStream bout = new        BufferedOutputStream( new
 
    FileOutputStream(fileName));
while ((read = bin.read(buffer)) > - 1 ) {
bout.write(buffer, 0 , read);
}
   bout.flush();
    bout.close();
   bin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
 
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(newFile( "XXXXmp3的文件全路径"
 
)), "audio/*" );
startActivity(intent);

4. getSharedPreferences 与 getPreferences 的区别。

getSharedPreferences是Context类中的方法,可以指定file name 以及 mode。

getPreferences是Activity类中的方法,只需指定mode

5.scrollview 滚动到顶部

?
1
2
3
4
5
6
7
8
scrollView.post( new Runnable() {
 
@Override
public void run() {
scrollView.scrollTo( 0 , 0 );
}
 
});

6.Android AlertDialog详解

一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的小功能.
对话框对象种类:
警告对话框AlertDialog:  一个可以有0到3个按钮, 一个单选框或复选框的列表的对话框. 警告对话框可以创建大多数的交互界面, 是推荐的类型.
进度对话框ProgressDialog:  显示一个进度环或者一个进度条. 由于它是AlertDialog的扩展, 所以它也支持按钮.
日期选择对话框DatePickerDialog:  让用户选择一个日期.
时间选择对话框TimePickerDialog:  让用户选择一个时间.
如果你希望自定义你的对话框, 可以扩展Dialog类.
Showing a Dialog 显示对话框
一个对话框总是被创建和显示为一个Activity的一部分. 你应该在Activity的onCreateDialog(int)中创建对话框. 当你使用这个回调函数时,Android系统自动管理每个对话框的状态并将它们和Activity连接, 将Activity变为对话框的”所有者”. 这样,每个对话框从Activity继承一些属性. 例如,当一个对话框打开时, MENU键会显示Activity的菜单, 音量键会调整Activity当前使用的音频流的音量.
注意: 如果你希望在onCreateDialog()方法之外创建对话框, 它将不会依附在Activity上. 你可以使用setOwnerActivity(Activity)来将它依附在Activity上.
当你希望显示一个对话框时, 调用showDialog(int)并将对话框的id传给它.
当一个对话框第一次被请求时,Android调用onCreateDialog(int). 这里是你初始化对话框的地方. 这个回调函数传入的id和showDialog(int)相同. 创建对话框之后,将返回被创建的对象.
在对话框被显示之前,Android还会调用onPrepareDialog(int, Dialog). 如果你希望每次显示对话框时有动态更改的内容, 那么就改写这个函数. 该函数在每次一个对话框打开时都调用. 如果你不定义该函数,则对话框每次打开都是一样的. 该函数也会传入对话框的id以及你在onCreateDialog()中创建的Dialog对象.
最好的定义onCreateDialog(int) 和onPrepareDialog(int, Dialog) 的方法就是使用一个switch语句来检查传入的id. 每个case创建相应的对话框. 例如, 一个游戏使用两个对话框: 一个来指示游戏暂停,另一个指示游戏结束. 首先, 为它们定义ID:static final int DIALOG_PAUSED_ID = 0;
static final int DIALOG_GAMEOVER_ID = 1; 
然后, 在onCreateDialog(int)中加入一个switch语句:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
protected Dialog onCreateDialog( int id) {
    Dialog dialog;
    switch (id) {
    case DIALOG_PAUSED_ID:
        // do the work to define the pause Dialog
        break ;
    case DIALOG_GAMEOVER_ID:
        // do the work to define the game over Dialog
        break ;
    default :
        dialog = null ;
    }
    return dialog;
}

注意: 在这个例子中, case语句为空因为定义Dialog的程序在后面会有介绍.
在需要显示对话框是, 调用showDialog(int), 传入对话框的id:
showDialog(DIALOG_PAUSED_ID);Dismissing a Dialog 解除对话框
当你准备关闭对话框时, 你可以使用dismiss()函数. 如果需要的话, 你也可以从Activity调用dismissDialog(int), 二者效果是一样的.
如果你使用onCreateDialog(int)来管理你的对话框的状态, 那么每次你的对话框被解除时, 该对话框对象的状态会被Activity保存. 如果你决定你不再需要这个对象或者需要清除对话框的状态, 那么你应该调用 removeDialog(int). 这将把所有该对象的内部引用移除, 如果该对话框在显示的话将被解除.
Using dismiss listeners 使用解除监听器
如果你希望在对话框解除时运行某些程序, 那么你应该给对话框附加一个解除监听器.
首先定义DialogInterface.OnDismissListener接口. 这个接口只有一个方法, onDismiss(DialogInterface), 该方法将在对话框解除时被调用.
然后将你的OnDismissListener实现传给setOnDismissListener().
然而,注意对话框也可以被”取消”. 这是一个特殊的情形, 它意味着对话框被用户显式的取消掉. 这将在用户按下”back”键时,或者对话框显式的调用cancel()(按下对话框的cancel按钮)时发生. 当一个对话框被取消时, OnDismissListener将仍然被通知, 但如果你希望在对话框被显示取消(而不是正常解除)时被通知, 则你应该使用setOnCancelListener()注册一个DialogInterface.OnCancelListener.
Creating an AlertDialog 创建警告对话框
An AlertDialog is an extension of the Dialog class. It is capable of constructing most dialog user interfaces and is the suggested dialog type. You should use it for dialogs that use any of the following features:
一个警告对话框是对话框的一个扩展. 它能够创建大多数对话框用户界面并且是推荐的对话框类新星. 对于需要下列任何特性的对话框,你都应该使用它:
一个标题
一条文字消息
1个-3个按钮
一个可选择的列表(单选框或者复选框)
要创建一个AlertDialog, 使用AlertDialog.Builder子类. 使用AlertDialog.Builder(Context)来得到一个Builder, 然后使用该类的公有方法来定义AlertDialog的属性. 设定好以后, 使用create()方法来获得AlertDialog对象.
下面的主题展示了如何为AlertDialog定义不同的属性, 使用AlertDialog.Builder类. 如果你使用这些示例代码, 你可以在onCreateDialog()中返回最后的Dialog对象来获得图片中对话框的效果.
Adding buttons 增加按钮

 

7. android 在有对话框的情况下,响应 activity 的返回键 (如何在alertdialog中响应  返回键)

我有一个activity,在其中只是弹出了一个alertdialog。这时候我需要能够响应到一些按键操作。比如说 back按键。这时候我需要使用的是alertdialog.setOnKeyListener()来监听back按键。(此时在activity中重载onKeyDown()是响应不到back按键的,因为alertdialog是模态的,阻塞了按键消息的传递,一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点 )但是我现在想在这样的情况下响应 back 按键,少说废话,上代码:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
AlertDialog dialog;
dialog= new AlertDialog.Builder(LoadingActivity. this )
.setView(layout).show();
 
dialog.setOnKeyListener( new OnKeyListener(){
   public boolean onKey(DialogInterface arg0,
 
int keyCode, KeyEvent arg2) {
  if (keyCode == KeyEvent.KEYCODE_BACK) {
   dialog.dismiss();
  finish();
  }
return true ;
}} );        问题解决!!

 

 

8. 手机屏幕截图:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 获取和保存当前屏幕的截图
*/
private Bitmap GetandSaveCurrentImage()
{
//1.构建Bitmap
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
//2.获取屏幕
View decorview = this .getWindow().getDecorView();
decorview.setDrawingCacheEnabled( true );
Bmp = decorview.getDrawingCache();
return Bmp;
}

 

9. 获取SDCard的目录路径功能  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 获取SDCard的目录路径功能
* @return
*/
private String getSDCardPath(){
File sdcardDir = null ;
//判断SDCard是否存在
boolean sdcardExist = Environment.getExternalStorageState()
 
.equals(android.os.Environment.MEDIA_MOUNTED);
 
if (sdcardExist){
sdcardDir = Environment.getExternalStorageDirectory();
}
return sdcardDir.toString();
}

10  给图片加水印

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 给图片添加水印
private Bitmap createBitmap(Bitmap src, String str) {
Time t = new Time();
t.setToNow();
int w = src.getWidth();
int h = src.getHeight();
String mstrTitle = "截图时间:" +t.hour + ":" + t.minute
 
+ ":" + t.second;
Bitmap bmpTemp = Bitmap.createBitmap(w, h,
 
Config.ARGB_8888);
Canvas canvas = new Canvas(bmpTemp);
Paint p = new Paint();
String familyName = "宋体" ;
Typeface font = Typeface.create(familyName,
 
Typeface.BOLD);
p.setColor(Color.BLUE);
p.setTypeface(font);
p.setTextSize( 22 );
canvas.drawBitmap(src, 0 , 0 , p);
canvas.drawText(mstrTitle, 0 , 20 , p);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return bmpTemp;
}

11. 开启前置 摄像头

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 要提示用户 2.3 版本以上的 才能支持 前置摄像头
<span style= "color: #ff0000;" > int cameraCount = 0 ; </span>
<span style= "color: #ff0000;" >Camera.CameraInfo cameraInfo = new Camera.</span>
 
<span style= "color: #ff0000;" >CameraInfo(); </span>
<span style= "color: #ff0000;" >cameraCount =</span>
 
<span style= "color: #ff0000;" >Camera.getNumberOfCameras(); // get cameras number </span>
<span style= "color: #ff0000;" > for ( int camIdx = 0 ; camIdx &lt; cameraCount;camIdx++ ) { </span>
<span style= "color: #ff0000;" >Camera.getCameraInfo( camIdx,</span>
 
<span style= "color: #ff0000;" >cameraInfo ); // get camerainfo </span>
<span style= "color: #ff0000;" > if ( cameraInfo.facing ==Camera.CameraInfo.</span>
 
<span style= "color: #ff0000;" >CAMERA_FACING_FRONT ) { // 代表摄像头的方位,目前有定义值两个分别为CAMERA_FACING_FRONT前置和CAMERA_FACING_BACK后置 </span>
<span style= "color: #ff0000;" > try { </span>
<span style= "color: #ff0000;" >camera = Camera.open( camIdx ); </span>
<span style= "color: #ff0000;" >} catch (RuntimeException e) { </span>
<span style= "color: #ff0000;" >e.printStackTrace(); </span>
<span style= "color: #ff0000;" >} </span>
<span style= "color: #ff0000;" >} </span>
<span style= "color: #ff0000;" >}</span>
System.out.println(Build.VERSION.SDK_INT);
// camera=Camera.open();     这是开启后置摄像头,  把注视去掉,把红色的代码去掉 就开启了 后置摄像头
Camera.Parameters parameters=camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setPreviewSize( 320 , 240 );
parameters.setPictureSize( 320 , 240 );
camera.setParameters(parameters);
camera.startPreview(); //开始预览

12. 保存图片

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//保存Bitmap
try {
File path = new File(SavePath);
//文件
String filepath = SavePath + "/Screen_1.png" ;
File file = new File(filepath);
if (!path.exists()){
path.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
 
FileOutputStream fos = null ;
fos = new FileOutputStream(file);
if ( null != fos) {
Bmp.compress(Bitmap.CompressFormat.PNG, 90 , fos);
fos.flush();
fos.close();
 
Toast.makeText(mContext, "截屏文件已保存至
 
SDCard/AndyDemo/ScreenImage/下", Toast.LENGTH_LONG).show();
}
 
} catch (Exception e) {
e.printStackTrace();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值