2016-04-08 32 views
0

如何獲取當前佈局的位圖(用於將圖像共享爲圖像)而不是截圖?我正在使用以下行獲取佈局視圖, 我可以獲取此佈局的位圖,但圖像看起來像是空白的(我有空白圖像)。這是我的代碼。如何獲取當前佈局的位圖(用於將屏幕共享爲圖像)而不是截圖?

View cardShareView = getLayoutInflater().inflate(R.layout.activity_cad_profile, null); 

boolean imageResult=saveImageInLocal(getBitmapFromView(cardShareView), "cad_share.png") == true 

public static Bitmap getBitmapFromView(View view) { 
    view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); 
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), 
      Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(bitmap); 
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 
    view.draw(canvas); 
    return bitmap; 
} 

private boolean saveImageInLocal(Bitmap imageData, String filename) { 
    String iconsStoragePath = Environment.getExternalStorageDirectory() + "/KKMC/"; 
    File sdIconStorageDir = new File(iconsStoragePath); 
    //create storage directories, if they don't exist 
    sdIconStorageDir.mkdirs(); 

    try { 
     filePath = sdIconStorageDir.toString() + filename; 
     FileOutputStream fileOutputStream = new FileOutputStream(filePath); 

     BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
     //choose another format if PNG doesn't suit you 
     imageData.compress(Bitmap.CompressFormat.PNG, 70, bos); 

     bos.flush(); 
     bos.close(); 

    } catch (FileNotFoundException e) { 
     Log.w("TAG", "Error saving image file: " + e.getMessage()); 
     return false; 
    } catch (IOException e) { 
     Log.w("TAG", "Error saving image file: " + e.getMessage()); 
     return false; 
    } 

    return true; 
} 

高級感謝您的回覆。

this image期待位 I got this image

+0

後ur代碼,可能是因爲你的圖片加載器... – appukrb

回答

0

最後,我創建重複的佈局(activity_cad_profile.xml),用於取全視圖,其在主佈局時加載的加載圖像。我不知道這是否正確。但它對我來說工作得很好。