0

我需要将大视图说(宽度和高度为 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)
4

2 回答 2

0

嗨,请看一下这段代码。

public static Bitmap new_decode(File f) {

    // decode image size

    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    o.inDither = false; // Disable Dithering mode

    o.inPurgeable = true; // Tell to gc that whether it needs free memory,
                            // the Bitmap can be cleared

    o.inInputShareable = true; // Which kind of reference will be used to
                                // recover the Bitmap data after being
                                // clear, when it will be used in the future
    try {
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // Find the correct scale value. It should be the power of 2.
    final int REQUIRED_SIZE = 300;
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 1.5 < REQUIRED_SIZE && height_tmp / 1.5 < REQUIRED_SIZE)
            break;
        width_tmp /= 1.5;
        height_tmp /= 1.5;
        scale *= 1.5;
    }

    // decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    // o2.inSampleSize=scale;
    o.inDither = false; // Disable Dithering mode

    o.inPurgeable = true; // Tell to gc that whether it needs free memory,
                            // the Bitmap can be cleared

    o.inInputShareable = true; // Which kind of reference will be used to
                                // recover the Bitmap data after being
                                // clear, when it will be used in the future
    // return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    try {

//      return BitmapFactory.decodeStream(new FileInputStream(f), null,
//              null);
        Bitmap bitmap= BitmapFactory.decodeStream(new FileInputStream(f), null, null);
        System.out.println(" IW " + width_tmp); 
        System.out.println("IHH " + height_tmp);           
        //   int iW = width_tmp;
         //   int iH = height_tmp;
       int iW = Talk_Library.screen_width;
        int iH = Talk_Library.screen_height;


           return Bitmap.createScaledBitmap(bitmap, iW, iH, true);

    } catch (OutOfMemoryError e) {
        // TODO: handle exception
        e.printStackTrace();
        // clearCache();

        // System.out.println("bitmap creating success");
        System.gc();
        return null;
        // System.runFinalization();
        // Runtime.getRuntime().gc();
        // System.gc();
        // decodeFile(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

}
于 2013-02-23T09:20:22.187 回答
0

尝试以下操作(即使他们查看已禁用其绘图缓存也可以使用):

int width = view.getWidth();
int height = view.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
// The view has now drawn to `bitmap`
于 2013-01-21T09:24:08.987 回答