1
我試圖截取SurfaceView的屏幕截圖,但它始終是黑屏的結果。我一直在尋找,但我發現的所有解決方案都不適用於我。這是我的代碼。在Android中拍攝SurfaceView的屏幕截圖
這是我稱之爲採取截圖
private void takePicture(){
if(camera!=null) {
camera.takePicture(null, null, jpgCb);
}
}
這是JPG回調
jpgCb=new Camera.PictureCallback() {
@Override
public void onPictureTaken(final byte[] data, final Camera camera) {
btnAccept.setVisibility(View.VISIBLE);
btnCancel.setVisibility(View.VISIBLE);
btnFlash.setEnabled(false);
btnAccept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopCamera();
new AsyncTask<byte[], String, Void>() {
@Override
protected void onPreExecute() {
progressDialog=new ProgressDialog(CameraActivity.this);
progressDialog.setCancelable(false);
progressDialog.setTitle(getResources().getString(R.string.saving_process));
progressDialog.show();
}
@Override
protected void onProgressUpdate(String... values) {
progressDialog.setMessage(values[0]);
}
@Override
protected Void doInBackground(byte[]... params) {
byte[] data=params[0];
File fileDir=new File(Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator+"folder"+File.separator+folderName);
if (!fileDir.exists())
fileDir.mkdirs();
File tmpFile=new File(fileDir,"tmpFile.jpeg");
try{
FileOutputStream fos=new FileOutputStream(tmpFile);
fos.write(data);
fos.close();
}catch (FileNotFoundException e){
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
}catch (IOException e){
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
}
BitmapFactory.Options options=new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
tmpFile.delete();
takeScreenshot();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
try{
pics=loadPics();
setGallery();
}catch (NullPointerException e){
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),getResources().getString(R.string.image_saved),Toast.LENGTH_SHORT).show();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
startCamera(w,h);
}else
startCamera();
btnAccept.setVisibility(View.GONE);
btnCancel.setVisibility(View.GONE);
btnFlash.setEnabled(true);
progressDialog.dismiss();
}
}.execute(data);
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopCamera();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
startCamera(w,h);
}else
startCamera();
btnAccept.setVisibility(View.GONE);
btnCancel.setVisibility(View.GONE);
btnFlash.setEnabled(true);
}
});
}
};
這是我在回調調用真正的方法拍截圖
public void takeScreenshot(){
cameraContainer.setDrawingCacheEnabled(true);
cameraContainer.buildDrawingCache(true);
Bitmap bmp=Bitmap.createBitmap(cameraContainer.getDrawingCache());
cameraContainer.setDrawingCacheEnabled(false);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG,qualityValue,bos);
byte[] bitmapData=bos.toByteArray();
ByteArrayInputStream fis=new ByteArrayInputStream(bitmapData);
String fileName=System.currentTimeMillis()+".jpeg";
File fileDir=new File(Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator+"folder"+File.separator+folderName);
if (!fileDir.exists())
fileDir.mkdirs();
try{
File tmpFile=new File(fileDir,fileName);
FileOutputStream fos=new FileOutputStream(tmpFile);
byte[] buf=new byte[1024];
int len;
while ((len=fis.read(buf))>0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
使用此功能,我可以將「SurfaceView內容」保存在文件中並存儲,但內容爲全黑屏幕。
PS:這是試圖解決我的問題,當我最發現的問題(事實上,「takeScreenshot」方法與答案)
編輯:這是stopCamera方法
private void stopCamera(){
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera=null;
}
請編輯並添加stopCamera源代碼 –
可能的重複http://stackoverflow.com/questions/27817577/android-take-screenshot-of-surface-view-shows-black-screen/ – fadden