屏幕截图不适用于android

时间:2014-04-05 05:22:24

标签: android bitmap screenshot

这是我的代码。我想在点击按钮时截取屏幕截图并在iamgeview中预览图像并存储在SD卡中。但是当点击按钮时没有任何事情发生!!我对androdi.plz有所帮助!提前感谢

case R.id.screnshotid:
    Bitmap bitmap;
    View v1 = findViewById(R.id.Flayout);// get ur root view id
    v1.setDrawingCacheEnabled(true); 
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "test.jpg");
    try {
        f.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    FileOutputStream fo = null;
    try {
        fo = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    try {
        fo.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    break;

1 个答案:

答案 0 :(得分:1)

  Bitmap bitmap;
  View v1 = findViewById(R.id.rlid);// get ur root view id
  v1.setDrawingCacheEnabled(true); 
  bitmap = Bitmap.createBitmap(v1.getDrawingCache());
  v1.setDrawingCacheEnabled(false);

保存

  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();

修改

ok.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            imageView.buildDrawingCache();
            Bitmap bm = imageView.getDrawingCache();

            OutputStream fOut = null;
            Uri outputFileUri;
            try {
                File root = new File(Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "folder_name" + File.separator);
                root.mkdirs();
                File sdImageMainDirectory = new File(root, "myPicName.jpg");
                outputFileUri = Uri.fromFile(sdImageMainDirectory);
                fOut = new FileOutputStream(sdImageMainDirectory);
            } catch (Exception e) {
            }
            try {
                bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();
            } catch (Exception e) {
            }
        }
    });

不要忘记添加权限,否则它无法正常工作:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

默认情况下,它会将图像保存在SD卡的文件夹中,文件夹名称为folder_name且名称为test.jpg