2016-03-25 14 views
2

私は基本的に、アクティビティで相対レイアウトのスクリーンショットをキャプチャしてPNG画像として保存しようとしています。次に、ビットマップファイルとして取得する必要があります。これを行うためのコードを書きましたが、残念ながらスクリーンショットはデバイスに保存されません。Android:ビューのスクリーンショットを撮る、画像を保存して取得する

XML

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/parent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- Other views --> 

</RelativeLayout> 

コード:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_view); 

     //The view that needs to be captured as an image 
     parent = (RelativeLayout) findViewById(R.id.parent); 

     Bitmap image = getBitmapFromView(parent); 
} 

public static Bitmap getBitmapFromView(View view) { 
     Date now = new Date(); 
     android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); 
     Bitmap returnBitmap = null; 

     try { 

      String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg"; 

      view.setDrawingCacheEnabled(true); 
      Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); 
      view.setDrawingCacheEnabled(false); 

      File imageFile = new File(mPath); 

      FileOutputStream outputStream = new FileOutputStream(imageFile); 
      int quality = 100; 
      bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); 
      outputStream.flush(); 
      outputStream.close(); 

      //Retrieve the image as a Bitmap and return it 
      if(imageFile.exists()){ 
       returnBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 
      } 

     } catch (Throwable e) { 
      // Several error may come out with file handling or OOM 
      e.printStackTrace(); 
     } 
     return returnBitmap; 
    } 

上記のコードは動作しません。エラーはありませんが、ビューはキャプチャされずに保存されます。誰かが問題を指摘したり、より良い解決策を教えてくれますか?

+0

希望

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

この権限を追加することを忘れないでください誰かが質問のコードセクションを編集してくださいことはできますか?バグがあり、コード内で編集できないようです。 – Dinuka

+0

@Baronz質問者を編集していただきありがとうございます – Dinuka

答えて

1
  1. まずfindViewById() を使用して参照を取得します

  2. を懸念ビューにIDを割り当てる次に、このコードスニペットをたどる

//ビューの高さと幅でビットマップを定義します。

Bitmap viewBitmap = Bitmap.createBitmap(v.getWidth(),v.getHeight(),Bitmap.Config.RGB_565); 

Canvas viewCanvas = new Canvas(viewBitmap); 

//get background of canvas 
Drawable backgroundDrawable = v.getBackground(); 

if(backgroundDrawable!=null){ 
backgroundDrawable.draw(viewCanvas);//draw the background on canvas; 
} 
else{ 
viewCanvas.drawColor(Color.GREEN); 
//draw on canvas 
v.draw(viewCanvas) 
} 

//write the above generated bitmap to a file 
String fileStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     OutputStream outputStream = null; 
     try{ 
      imgFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),fileStamp+".png"); 
      outputStream = new FileOutputStream(imgFile); 
      b.compress(Bitmap.CompressFormat.PNG,40,outputStream); 
      outputStream.close(); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 

PNGをJPEG

は、このことができます:)

関連する問題