android中的屏幕截图

android中的屏幕截图,android,screenshot,Android,Screenshot,我想通过一个程序在我的android活动中截图,这样当用户点击按钮截图时,截图应该保存在SD卡中。在eclipse中,代码中的包名用红线TOSTRING(com.meri.meri_应用程序)划线 公共类PartySalesandRecovery扩展活动实现OnClickListener{ 用户函数用户函数; 数据库处理程序数据库; 字符串str; 字符串moneyString2、moneyString3、moneyString4、moneyString5、moneyString6、moneyS

我想通过一个程序在我的android活动中截图,这样当用户点击按钮截图时,截图应该保存在SD卡中。在eclipse中,代码中的包名用红线TOSTRING(com.meri.meri_应用程序)划线

公共类PartySalesandRecovery扩展活动实现OnClickListener{
用户函数用户函数;
数据库处理程序数据库;
字符串str;
字符串moneyString2、moneyString3、moneyString4、moneyString5、moneyString6、moneyString7;
字符串月份、登录名、周;
图像视图图像视图;
位图bmp;
智力定向;
int总销售额=0;
国际总销售额=0;
int total_rec_trg=0;
int total_rec_act=0;
每笔销售的整数总计=0;
int total_per_rec=0;
私有静态字符串密钥\u SUCCESS=“SUCCESS”;
表格布局tl;
LayoutParams有限公司;
斯宾纳·克斯韦克,克斯蒙特;
按钮BTN显示,BTN屏幕;
JSONObject json,json_用户;
私人视野;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.partysalesandrecovery);
视图=findViewById(R.id.screenRoot);
方向=this.getResources().getConfiguration().orientation;
Calendar cal=Calendar.getInstance();
SimpleDateFormat月\日=新SimpleDateFormat(“mmmmmmmm”);
字符串month\u name=month\u date.format(cal.getTime());
quesMonth=(微调器)findViewById(R.id.comboMonth);
quesWeek=(微调器)findViewById(R.id.comboWeek);
ArrayAdapter myAdap=(ArrayAdapter)quesMonth.getAdapter();
int spinnerPosition=myAdap.getPosition(月份名称);
quesMonth.setSelection(喷丝头位置);
tl=(TableLayout)findViewById(R.id.tableLayoutLocationSaleandRecovery);
btnShow=(按钮)findViewById(R.id.btnShowReports);
setOnClickListener(这个);
btnTakeScreen=(按钮)findViewById(R.id.btnTakeScreenShot);
btnTakeScreen.setOnClickListener(此);
}
///////////////////////////////
///////////////////////////////////////
公共void保存位图(位图){
试一试{
ByteArrayOutputStream字节=新建ByteArrayOutputStream();
compress(bitmap.CompressFormat.JPEG,40,字节);
文件f=新文件(Environment.getExternalStorageDirectory()
+File.separator+“test.jpg”);
f、 createNewFile();
FileOutputStream fo=新的FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
}catch(filenotfounde异常){
Log.e(“GREC”,e.getMessage(),e);
}捕获(IOE异常){
Log.e(“GREC”,e.getMessage(),e);
}
}
////////////////////////////////
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
if(v.getId()==R.id.btnTakeScreenShot)
{
位图Bitmap=Bitmap.createBitmap(500500,Bitmap.Config.ALPHA_8);
查看自=(LinearLayout)findViewById(R.id.viewRoot);
位图结果=drawViewToBitmap(从,位图);
imageView=(imageView)findViewById(R.id.imgViewpic);
设置图像位图(结果);
保存位图(位图);
尝试
{
imageView.setDrawingCacheEnabled(true);
bmp=Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(false);
ByteArrayOutputStream字节=新建ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG,40,字节);
文件f=新文件(Environment.getExternalStorageDirectory()
+File.separator+“test.jpg”);
f、 createNewFile();
FileOutputStream fo=新的FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
Toast.makeText(this,e.getMessage(),7000.show();
} 
}
XML代码
您可以尝试以下方法:

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

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

OutputStream fout = null;
imageFile = new File(mPath);

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
这个答案来自:

说明: 具有图像名称的输出图像路径:

String mPath = Environment.getExternalStorageDirectory().getPath() + "/" + "ScreenShot.jpg";
获取当前视图/显示的屏幕图像,并从此创建位图:

Bitmap bitmap;
View v1 = getWindow().getDecorView().getRootView(); // View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
最后保存并压缩图像:

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

}

只需将保存位图方法替换为

public void saveBitmap(Bitmap bitmap) {

    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "test.jpg");
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}
编辑1:

只要用这个代码来回复你的代码

    @Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId() == R.id.btnTakeScreenShot) {
        Bitmap bitmap = takeScreenshot()
        imageView.setImageBitmap(bitmap);

        saveBitmap(bitmap);


    }
}


public Bitmap takeScreenshot() {
    View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {

    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "test.jpg");
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

如果你要面对这些错误,我们为什么不呢?还有,你是想用单引号括起你的代码吗?这个链接将帮助你解决问题谢谢分配MEENAL SHARMA和JEREMY不,我没有在我的程序中用单代码关闭代码..当我添加包装名称meri_应用程序时出现错误你尝试过我提到的链接吗..@MEENAL Sharma是的,我查看过,想知道它会将图片保存在文件夹中,但我无法查看,也不知道它将屏幕截图保存在哪里我找到了文件夹,但没有图像我已经查看过,但我无法理解它有点复杂,或者可能我没有正确理解它,但不知何故,这就是问题所在请看我的解释…我想这会对你有一点帮助。请告诉我屏幕截图将保存在哪里,我将在哪里找到我的文件夹。我如何知道屏幕截图被捕获?将其检查到设备根目录中。Environment.getExternalStorageDirectory().getPath()使用返回设备根目录(外部SD卡)。仍然找不到屏幕截图图像直到找不到屏幕截图的图像您已实现与相同的代码
    @Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId() == R.id.btnTakeScreenShot) {
        Bitmap bitmap = takeScreenshot()
        imageView.setImageBitmap(bitmap);

        saveBitmap(bitmap);


    }
}


public Bitmap takeScreenshot() {
    View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {

    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "test.jpg");
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}