2

我有以下方法可以在操作项单击时捕获屏幕。它适用于 Android <2.3 但不适用于 4+。这种截屏方式有什么问题。

private void captureScreen() {
    View v = mapView.getRootView();
    v.setDrawingCacheEnabled(true);
    Bitmap capturedBitmap = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);

    if(capturedBitmap != null) {
        Intent intent = new Intent(this, ScreenCapturedAlertActivity.class);
        intent.putExtra("capturedImage", capturedBitmap);
        intent.putExtra("name", location.getName());
        startActivity(intent);
    } else {
        Toast.makeText(this, "Screen Capture Failed", Toast.LENGTH_SHORT).show();
    }
}

ScreenCaputureAlertActivity.java >>>

public class ScreenCapturedAlertActivity extends SherlockActivity {

private ImageView capturedImage;
private Bitmap capturedBitmap;
private String name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_screencaptured_alert);

    capturedBitmap = (Bitmap) getIntent().getParcelableExtra("capturedImage");
    name = getIntent().getStringExtra("name");

    capturedImage = (ImageView) findViewById(R.id.ivCapturedImage);
    capturedImage.setImageBitmap(capturedBitmap);
}

private void saveAndShare(boolean share) {
    String root = Environment.getExternalStorageDirectory().toString();
    File dir = new File(root + "/capture/");
    if(!dir.exists())
        dir.mkdirs();

    FileOutputStream outStream = null;
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    File file = new File(dir, "Capture "+n+".jpg");
    if(file.exists()) {
        file.delete();
    }
    try {
        outStream = new FileOutputStream(file);

        capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

        outStream.flush();
        outStream.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
        return;
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
        return;
    }

    if(share) {
        Uri screenshotUri = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
        intent.putExtra(Intent.EXTRA_SUBJECT, "Location of " + name);
        intent.putExtra(Intent.EXTRA_TITLE, getText(R.string.screen_share_message));
        intent.putExtra(Intent.EXTRA_TEXT, getText(R.string.screen_share_message));
        intent.setType("image/*");
        startActivity(Intent.createChooser(intent, "Share with"));
        finish();
    } else {
        Toast.makeText(this, "Save Success", Toast.LENGTH_SHORT).show();

        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        finish();
    }
}

public void saveCapture(View view) {
    saveAndShare(false);
}

public void shareCapture(View view) {
    saveAndShare(true);
}

}

4

2 回答 2

2

感谢@KumarBibek 的指导。

我得到的错误是

!!! FAILED BINDER TRANSACTION !!!

从链接中选择的答案

将位图作为字节数组发送

我在第一个活动中确实喜欢这样:

private void captureScreen() {
    View v = mapView.getRootView();
    v.setDrawingCacheEnabled(true);
    Bitmap capturedBitmap = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    capturedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    if(capturedBitmap != null) {
        Intent intent = new Intent(this, ScreenCapturedAlertActivity.class);
        intent.putExtra("capture", byteArray);
        intent.putExtra("name", location.getName());
        startActivity(intent);
    } else {
        Toast.makeText(this, "Screen Capture Failed", Toast.LENGTH_SHORT).show();
    }

}

在 ScreenCapturedAlertActivity 中:

byte[] byteArray = getIntent().getByteArrayExtra("capture");
        capturedBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

它现在运行良好。再次感谢@KumarBibek

于 2013-03-18T07:50:17.240 回答
1

不要传递整个位图,而是尝试将文件保存文件的路径传递给下一个活动。位图是一个大对象,不应该像那样传递。

由于您已经检查了图像是否保存完好,如果您处理路径而不是位图,我认为它会解决您的问题。

于 2013-03-18T07:34:34.467 回答