2013-01-21 8 views
0

큰보기 (너비와 높이 1280x1760)를 비트 맵으로 변환하고 이미지를 sdcard.But에 저장해야합니다. 크기가 1280x1400 이상인보기를 만들 때 내 응용 프로그램이 예외를 throw합니다. 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> 

가 로그 캣

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) 
+2

귀하의 logcat을 게시하십시오. –

+0

위의 logcat을 찾으십시오. 문제는 view.getDrawingCache()가 null을 반환한다고 생각합니다. –

답변

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` 
+0

위의 코드를 사용하여 원하는 크기의 비트 맵을 만들었으나 비트 맵에 전체 뷰를 그리는 방법을 설명했습니다. 문제 –

+0

비트 맵에 크기 (1280x1760)의 전체보기를 그릴 수있는 방법을 알려주십시오. 또는 전체보기를 비트 맵으로 변환하는 다른 방법이 있습니까? –

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; 
    } 

} 
관련 문제