0%

Android笔记


将Assets中的文件拷贝到SD卡中

有时候需要将Assets中的资源共享给其他应用,但是Assets实际是在APK包体内,无法直接传递路径,这时就需要将文件复制到一个可以公共存取的地方。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void copyBigDataToSD(String strOutFileName) throws IOException {  
InputStream myInput;
String outpath = Environment. getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES ) + "/" + strOutFileName;
OutputStream myOutput = new FileOutputStream( outpath );
myInput = mActivity.getAssets().open( strOutFileName );
byte[] buffer = new byte[1024];
int length = myInput.read(buffer);
while(length > 0)
{
myOutput.write(buffer, 0, length);
length = myInput.read(buffer);
}
myOutput.flush();
myInput.close();
myOutput.close();
}

获得屏幕快照 Screenshot

用于游戏截图,但需要注意:如果有应用中使用了GLSurfaceView,那这个就不好使了,会得到黑色的截图。

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
private void takeScreenshot( String filename ) {
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment. getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES ) + "/" + filename + ".jpg";

// create bitmap screen capture
View v1 = mActivity.getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

File imageFile = new File(mPath);

FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();

openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}

//此方法调用图片查看器查看图片
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
mActivity.startActivity(intent);
}

利用ACTION_SEND实现分享功能

利用支持ACTION_SEND的应用分享内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void Share( ) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post"); //标题
sendIntent.putExtra(Intent.EXTRA_TEXT, "http://www.google.com"); //内容

//分享Assets内的图片
String img_path = "splash.jpg";
try {
copyBigDataToSD( img_path ); //将Assets中的图片复制到SD卡中 这样使其他的应用可以读取
} catch (IOException e) {
e.printStackTrace();
}
String imagePath = Environment. getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES ) + "/" + img_path;
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri); //只接受URI

//这个设置会影响应用筛选的结果
//sendIntent.setType("text/plain");
sendIntent.setType("image/jpeg");

mActivity.startActivityForResult( Intent.createChooser( sendIntent, "Share to" ), 0 );
}

保持屏幕不暗不关闭的官方推荐方法

调用 keepScrrenOn() 函数后,在当前Activity可见的情况下保持屏幕不暗不灭。

1
2
3
4
5
6
7
8
9
10
11
12
13
// Sets the flag to keep this screen on. It's recommended to do that during
// the
// handshake when setting up a game, because if the screen turns off, the
// game will be
// cancelled.
void keepScreenOn() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

// Clears the flag that keeps the screen on.
void stopKeepingScreenOn() {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}