2015-09-25 111 views
4

我想救我的活動的一部分,沒有工具欄和狀態欄。我現在擁有的代碼可以保存整個屏幕。請參考下面的圖片。保存一部分圖像

enter image description here

代碼,我現在所擁有的:

llIDCardRootView = (LinearLayout) view.findViewById(R.id.ll_id_card_root_view); 
     llIDCardContainer = (LinearLayout) llIDCardRootView.findViewById(R.id.ll_id_card_view); 

private void createBitmap() { 

     Log.d(Const.DEBUG, "Creating Bitmap"); 

     Bitmap bmp; 
     //View v = llIDCardContainer.getRootView(); 
     //View v = activity.getWindow().getDecorView().findViewById(android.R.id.content); 
     //View v = activity.findViewById(R.id.ll_id_card_root_view); 
     ViewGroup v = (ViewGroup) ((ViewGroup) activity 
       .findViewById(android.R.id.content)).getChildAt(0); 

     v.setDrawingCacheEnabled(true); 
//  v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), 
//    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
//  v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 
//  v.buildDrawingCache(true); 

     bmp = Bitmap.createBitmap(v.getDrawingCache()); 

     File directory = new File(Environment.getExternalStorageDirectory() 
       + File.separator); 
     File file = new File(directory, FILE_NAME); 

     try { 
      FileOutputStream out = new FileOutputStream(file); 
      bmp.compress(Bitmap.CompressFormat.JPEG, 100, out); 
      out.flush(); 
      out.close(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     v.destroyDrawingCache(); 
     v.setDrawingCacheEnabled(false); 
    } 

是保存圖像..

enter image description here

我如何才能挽救我需要的部分從片段?

+0

作物的形象和保存,這將是更好的 – Sree

回答

5

使用以下功能保存任何視圖到圖像文件。如果您需要保存在Fragment中,請在片段中調用以下函數。

public static Bitmap getBitmapFromView(View view) { 
     //Define a bitmap with the same size as the view 
     Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888); 
     //Bind a canvas to it 
     Canvas canvas = new Canvas(returnedBitmap); 
     //Get the view's background 
     Drawable bgDrawable =view.getBackground(); 
     if (bgDrawable!=null) 
      //has background drawable, then draw it on the canvas 
      bgDrawable.draw(canvas); 
     else 
      //does not have background drawable, then draw white background on the canvas 
      canvas.drawColor(Color.WHITE); 
     // draw the view on the canvas 
     view.draw(canvas); 
     //return the bitmap 
     return returnedBitmap; 
    } 
+0

謝謝有效 –

1

使用此

container_layout.setDrawingCacheEnabled(true); 
container_layout.buildDrawingCache(true); 
Bitmap saveBm = Bitmap.createBitmap(container_layout.getDrawingCache()); 
container_layout.setDrawingCacheEnabled(false); 

現在可以保存saveBm作爲文件

1

你必須做出一個單獨查看該保存爲圖像,然後你可以選擇的這種觀點就像截圖:

LinearLayout content = findViewById(R.id.rlid); 
content.setDrawingCacheEnabled(true); 
Bitmap bitmap = content.getDrawingCache(); 
File file,f;      
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
    { 
     file =new File(android.os.Environment.getExternalStorageDirectory(),"TTImages_cache"); 
     if(!file.exists()) 
     { 
      file.mkdirs(); 

     } 
     f = new File(file.getAbsolutePath()+file.seperator+ "filename"+".png"); 
    } 
    FileOutputStream ostream = new FileOutputStream(f);         
    bitmap.compress(CompressFormat.PNG, 10, ostream); 
    ostream.close(); 

} 
catch (Exception e){ 
e.printStackTrace(); 
} 

,不要忘了在manifest

添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

,你可以在這裏看到:HTTP://phpidiots.in/android/save-a-view-as-an-image-in-android/ –