2017-10-13 1 views
0

Ich muss meine Scroll alle Inhalte in Bild bei Android konvertieren, bevor ich Scroll Ansicht Höhe und Breite berechnen müssen. Wie man rechnet?Berechnen scrollview Höhe und Breite und Konvertieren scrollview Inhalt in Bild

Ich habe unten Code verwendet, aber meine App stürzt mit dem Fehler wie Höhe und Breite größer als Null ab.

View u = findViewById(R.id.trustReportScrollView); 
     ScrollView z = (ScrollView) findViewById(R.id.trustReportScrollView); 
     /* int totalHeight = z.getChildAt(0).getHeight(); 
     int totalWidth = z.getChildAt(0).getWidth();*/ 
    /* int totalHeight = 500; 
     int totalWidth = 500;*/ -->if i used this two line, i get image but empty white image, nothing in that image 

     Bitmap b = getBitmapFromView(u,totalHeight,totalWidth); -- >here i get crashed 

     //Save bitmap 
     String extr = Environment.getExternalStorageDirectory()+"/TestingDir1/"; 
     String fileName = "report.jpg"; 
     File myPath = new File(extr, fileName); 
     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream(myPath); 
      b.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
      fos.flush(); 
      fos.close(); 
      MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen"); 
     }catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
+0

Was ist eigentlich der Zweck ist? Die Sache, die du versuchst zu tun, ist ziemlich ungewöhnlich. –

+0

Ich muss mein Bild in PDF-Dokument für meine Anwendung konvertieren. –

Antwort

0

Check this out

int totalHeight = scrollview.getChildAt(0).getHeight(); 
      int totalWidth = scrollview.getChildAt(0).getWidth(); 
      try { 
       Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888); 

      } catch (Exception e) { 
       e.printStackTrace(); 

      } 
+0

danke für die Antwort, ich werde versuchen, –

+0

Es wird Ihnen den gesamten Scrollview Inhalt in Bitmap. –

+0

es gibt nicht, es zeigt kann keine Miniaturansicht erstellen, und leeres Bild –

0

Verwenden Sie den folgenden Code Ansicht blättern Inhalt Bild zu konvertieren.

//step :1 
    int height; 
    int width; 
    ScrollViewHelper iv; 

//step :2 
    iv = (ScrollViewHelper) findViewById(R.id.scrollViewHelper); 
    iv.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { 

       @Override 
       public void onScrollChanged() { 


        height = iv.getChildAt(0).getHeight(); 
        width = iv.getChildAt(0).getWidth(); 

       } 
    }); 

//step :3 
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
        Canvas ccc = new Canvas(bitmap); 
        iv.getChildAt(0).draw(ccc); 
        SaveImage(bitmap); 




//step :4 

    private void SaveImage(Bitmap finalBitmap) { 

      String root = Environment.getExternalStorageDirectory().toString(); 
     //File myDir = new File("/storage/emulated/0/DCIM"); 
      File myDir = new File(root+ File.separator + "Vengat.jpg"); 
      myDir.mkdirs(); 

      Random generator = new Random(); 
      int n = 10000; 
      n = generator.nextInt(n); 
      String fname = "Image"+ n +".jpg"; 
      File file = new File(myDir, fname); 

      if (file.exists()) 
       file.delete(); 
      try { 
       FileOutputStream out = new FileOutputStream(file); 
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 

       out.flush(); 

       out.close(); 


       File filePath =new File(file.toString()); //optional //internal storage 
       // File filePath =new File(Environment.getExternalStorageDirectory().toString()); //optional //internal storage 

       Intent shareIntent = new Intent(); 
       shareIntent.setAction(Intent.ACTION_SEND); 
       shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Open it in Google Play Store to Download the Application"); 
       shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(filePath)); //optional//use this when you want to send an image 
       shareIntent.setType("*/*"); 
       shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
       startActivity(Intent.createChooser(shareIntent, "send")); 



      } 
      catch (FileNotFoundException e) { 
       e.printStackTrace(); 
       Log.i("", "******* File not found. Did you" 
         + " add a WRITE_EXTERNAL_STORAGE permission to the manifest?"); 
      } 


      catch (Exception e) { 
       e.printStackTrace(); 
      } 


     } 

//////////////////////

<ScrollViewHelper 
android:id="@+id/scrollViewHelper" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<ScrollView 
android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    </ScrollView> 

    </ScrollViewHelper> 

////////////// // ScrollViewHelper: -

public class ScrollViewHelper erstreckt Scroll {

private OnScrollViewListener mOnScrollViewListener; 

    public ScrollViewHelper(Context context) { 
     super(context); 
    } 
    public ScrollViewHelper(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 
    public ScrollViewHelper(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public interface OnScrollViewListener { 
     void onScrollChanged(ScrollViewHelper v, int l, int t, int oldl, int oldt); 
    } 

    public void setOnScrollViewListener(OnScrollViewListener l) { 
     this.mOnScrollViewListener = l; 
    } 

    protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
     mOnScrollViewListener.onScrollChanged(this, l, t, oldl, oldt); 
     super.onScrollChanged(l, t, oldl, oldt); 
    } 
} 
+0

danke für die Antwort –

+0

Ich Scrollview verwendet, kann ich meine Scrollview ScrollViewHelper ändern? –

+0

der gleiche Fehler auftritt, verursacht durch: java.lang.IllegalArgumentException: Breite und Höhe müssen> 0 sein –