2015-03-02 70 views
2

我想从我的android应用拍摄一张照片,但由于我当前的Activity中有videoView,所以我得到一张黑色图像。我怎么能没有root权限?我怎样才能调用android截图系统?

波纹管代码即可获得我目前的观点:

在我的主要活动:

saveBitmap(tackeScreenshot());

和方法使用:

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


public void saveBitmap(Bitmap bitmap) { 

     File imagePath = new File("/sdcard/image.png"); 
     FileOutputStream fos; 
     try { 
      fos = new FileOutputStream(imagePath); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 
      fos.flush(); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      Functions.Log("saveBitmap", e.getMessage()); 
     } catch (IOException e) { 
      Functions.Log("saveBitmap", e.getMessage()); 
     } 
    } 
+1

参见[这个](http://stackoverflow.com/a/27454425/3531756)。 – 2015-03-02 12:35:27

+0

在'takeScreenshot()'中,你不会返回正确的类型。你应该返回一个**位图**。但是,您返回**视图**,而是。这不是'saveBitmap()'所期望的。 – 2015-03-02 12:48:32

回答

0

你应该做这样的事情

// 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 = 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(); 
} 

然后,当你需要访问使用这样的事情:

Uri uri = Uri.fromFile(new File(mPath));