`
l540151663
  • 浏览: 181077 次
  • 性别: Icon_minigender_1
  • 来自: 浙江
社区版块
存档分类
最新评论

android截图

阅读更多
  安卓有自带的截图方法,Activity如下:
package com.example.croppic;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

private ImageView ivImage;
private Button btnGet;
private static final int BITMAPCROP = 3021;
private File file;
Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ivImage = (ImageView) findViewById(R.id.ivShow);
btnGet = (Button) findViewById(R.id.btnGet);

String filename = android.os.Environment.getExternalStorageDirectory().getPath()
+ "/dcim/100MEDIA/test.jpg";

file = new File(filename);

btnGet.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

if (!file.exists()) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.l1);
dumpBitmap(bitmap);
}

Uri uri = Uri.fromFile(file);//从文件里获取图片uri
Intent intent = MainActivity.getCropImageIntent(uri);
startActivityForResult(intent, BITMAPCROP);
}
});

}

/**
* 保存图片
* @param b
*/
public void dumpBitmap(Bitmap b) {

if (b != null) {
FileOutputStream fos = null;
try {

fos = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

}
}
}

/**
*获取截图intent
* @param photoUri
* @return
*/
public static Intent getCropImageIntent(Uri photoUri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(photoUri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 1);//当xy为1,1时,表示截图框长宽比例1:1
// intent.putExtra("outputX", 300);
// intent.putExtra("outputY", 300);//输出的图片比例
intent.putExtra("return-data", true);
return intent;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;

if(file.exists()){
file.delete();
}

switch (requestCode) {

case BITMAPCROP:
Bitmap bm = data.getParcelableExtra("data");
if (bm != null) {
ivImage.setLayoutParams(new LinearLayout.LayoutParams(bm.getWidth(), bm.getHeight()));
ivImage.setImageBitmap(bm);

}
break;
}
}
}

xml布局文件activity_main.xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnGet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="GET"/>
   
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
    <ImageView
        android:id="@+id/ivShow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"/>
</LinearLayout>
   
</LinearLayout>

别忘了权限:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics