2012-10-18 43 views
1

我試圖將線性佈局轉換爲位圖。但它會給出一個空指針異常。我的代碼是。從線性佈局獲取位圖時的空指針

位圖爲空。因爲我沒有得到「不空」的敬酒。 爲什麼我得到的位圖爲null。我已經嘗試了很多類似的問題,但代碼也是一樣的。

public class Receipt extends Activity { 

    public Bitmap bitmap; 
    LinearLayout layout; 
    private String fileName; 
    private File file; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.receipt); 
     ImageView iv = (ImageView) findViewById(R.id.Ivsignature); 
     iv.setImageBitmap(Signature.sign); 

     layout = (LinearLayout) findViewById(R.id.Llreceipt); 
     layout.setDrawingCacheEnabled(true); 
     layout.buildDrawingCache(); 
     bitmap = layout.getDrawingCache(true); 

     if (bitmap != null) 
      Toast.makeText(getApplicationContext(), "not null", 
        Toast.LENGTH_LONG).show(); 
     String myPath = Environment.getExternalStorageDirectory() 
       + "/signature_image"; 
     File myDir = new File(myPath); 
     try { 
      myDir.mkdirs(); 
     } catch (Exception e) { 
     } 

     String fname = fileName + "gwg" + ".jpg"; 
     file = new File(myDir, fname); 
     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream(file); 
      bitmap.compress(CompressFormat.JPEG, 100, fos); 
      Toast.makeText(getApplicationContext(), "File saved", 
        Toast.LENGTH_LONG).show(); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), String.valueOf(e), 
        Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

回答

0
package com.example.snapshotlayout; 

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import android.os.Bundle; 
import android.os.Environment; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.view.View.MeasureSpec; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class SnapshotActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_snapshot); 

    //initializing layouts 
    TextView textView = (TextView)findViewById(R.id.textView); 
    ImageView snapImage = (ImageView)findViewById(R.id.imageView); 
    textView.setDrawingCacheEnabled(true); 

    //specifying the dimensions of view 
    textView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
      MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 

    textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight()); 

    textView.buildDrawingCache(true); 
    Bitmap b = Bitmap.createBitmap(textView.getDrawingCache()); 
    textView.setDrawingCacheEnabled(false); // clear drawing cache 

    snapImage.setImageBitmap(b); 

    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    b.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 

    //create a new file in sdcard name it as "testimage.jpg" 
    File fileImage = new File(Environment.getExternalStorageDirectory() 
          + File.separator + "testimage.jpg"); 

    //write the bytes in file 
    FileOutputStream fo = null; 
    try { 
     fileImage.createNewFile(); 
     fo = new FileOutputStream(fileImage); 
     fo.write(bytes.toByteArray()); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 

這裏是 http://hoodaandroid.blogspot.in/2012/09/taking-snapshot-or-screen-capture-of.html

+0

這億韓元沒有使用硬件加速,因爲drawingCache沒有使用,然後 –

+0

無處指定,他需要在:) –

+0

非常真實,只是想補充說,以防萬一誰讀這遇到這樣的問題 –

4

採取

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:id="@+id/snapLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<TextView 
    android:id="@+id/textView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Take snapshot of this layout"/> 
<ImageButton 
    android:id="@+id/imageView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

</LinearLayout> 

可以繪製你的看法與位圖實例化一個帆布的XML文件中的代碼,這樣的觀點將寫入位圖。繪製到該畫布後,位圖將包含該視圖。

請務必撥打以下後,您的觀點得到了中規定的功能,實際上具有寬度和高度

public Bitmap createBitmapForView(View view) 
{ 
    int width = view.getWidth(); 
    int height = view.getHeight(); 

    // create a bitmap the size of the view 
    Bitmap screenShot = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

    // create a canvas with the bitmap 
    Canvas canvas = new Canvas(screenShot); 

    // draw the view to the canvas 
    view.draw(canvas); 

    // now your bitmap contains the view 
    return screenShot; 
}