2013-01-21 3 views
0

J'ai besoin de convertir de grandes vues dites (largeur et hauteur comme 1280x1760) en Bitmap et enregistrer l'image sur sdcard.But Lorsque je crée des vues plus de la taille 1280x1400 mon application lance une exception wnen createBitmap (view.getDrawingCache()) est appelé. J'utilise l'onglet samsung 10 pouces. Est-ce que pixel d'image dépend de l'onglet pixel ici est mon codeAndroid Convert Agrandir la vue en bitmap

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(); 

         } 

et la mise en page est 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> 

ici est le 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) 
+2

Affichez votre logcat. –

+0

S'il vous plaît trouver le logcat ci-dessus je pense que le problème est avec view.getDrawingCache() qui renvoie null –

Répondre

0

Effectuez les opérations suivantes (qui fonctionne même si la vue a désactivé son cache de dessin):

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

J'ai déjà essayé le code ci-dessus, il a travaillé pour créer bitmap de la taille désirée, mais comment dessiner vue entière en bitmap ..... que la principale problème –

+0

s'il vous plaît certains peuvent me dire comment dessiner une vue entière de la taille (1280x1760) à Bitmap. Ou est-il une autre approche pour convertir la vue entière en bitmap ......... s'il vous plaît répondre –

0

Bonjour Veuillez jeter un coup d'œil à ce code.

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

}