我需要将大视图说(宽度和高度为 1280x1760)转换为位图并将图像保存到 sdcard。但是当我创建大小超过 1280x1400 的视图时,我的应用程序抛出异常 wnen createBitmap(view.getDrawingCache()) 被调用.我使用的是 10 英寸三星标签。图像像素是否取决于标签像素这是我的代码
View view2=LayoutInflater.from(getApplicationContext()).inflate(R.layout.sample, null);
LinearLayout view=new LinearLayout(getApplicationContext());
view.setBackgroundColor(Color.GRAY);
view.setOrientation(LinearLayout.VERTICAL);
view.addView(view2,new LayoutParams(1280,1760));
view.setDrawingCacheEnabled(true);
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
try{
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
//Bitmap bitmap = Bitmap.createScaledBitmap(view.getDrawingCache(), 1280, 1200, true);
//setContentView(view);
String path = Environment.getExternalStorageDirectory()+"/Pictures";
File image = new File(path, "Image7.jpg");
// Encode the file as a PNG image.
FileOutputStream outStream=null;
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
setContentView(view);
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), 10).show();
}
和 sample.xml 布局是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="300px"
android:text="TextView"
android:background="#ffff00"/>
<Button android:layout_weight="1"
android:id="@+id/button1" android:background="#ff00ff"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Button" />
<Button android:layout_weight="1" android:background="#ff0000"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Button" />
</LinearLayout>
这是 LOGCAT
01-21 14:26:50.381: E/AndroidRuntime(1611): FATAL EXCEPTION: main
01-21 14:26:50.381: E/AndroidRuntime(1611): java.lang.NullPointerException
01-21 14:26:50.381: E/AndroidRuntime(1611): at android.graphics.Bitmap.createBitmap(Bitmap.java:455)
01-21 14:26:50.381: E/AndroidRuntime(1611): at com.hunterlab.essentials.view2image.View2ImageActivity$1.Check(View2ImageActivity.java:196)
01-21 14:26:50.381: E/AndroidRuntime(1611): at com.hunterlab.essentials.view2image.View2ImageActivity$1.onClick(View2ImageActivity.java:79)
01-21 14:26:50.381: E/AndroidRuntime(1611): at android.view.View.performClick(View.java:3110)
01-21 14:26:50.381: E/AndroidRuntime(1611): at android.view.View$PerformClick.run(View.java:11928)
01-21 14:26:50.381: E/AndroidRuntime(1611): at android.os.Handler.handleCallback(Handler.java:587)
01-21 14:26:50.381: E/AndroidRuntime(1611): at android.os.Handler.dispatchMessage(Handler.java:92)
01-21 14:26:50.381: E/AndroidRuntime(1611): at android.os.Looper.loop(Looper.java:132)
01-21 14:26:50.381: E/AndroidRuntime(1611): at android.app.ActivityThread.main(ActivityThread.java:4025)
01-21 14:26:50.381: E/AndroidRuntime(1611): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 14:26:50.381: E/AndroidRuntime(1611): at java.lang.reflect.Method.invoke(Method.java:491)
01-21 14:26:50.381: E/AndroidRuntime(1611): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-21 14:26:50.381: E/AndroidRuntime(1611): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-21 14:26:50.381: E/AndroidRuntime(1611): at dalvik.system.NativeStart.main(Native Method)