2017-10-13 3 views
0

J'ai besoin de convertir mon affichage défiler tout le contenu en Image sur android avant de devoir calculer la hauteur et la largeur de la vue défilée. Comment calculer?Calculer la hauteur et la largeur du scrollview et convertir le contenu du scrollview en image

J'ai utilisé le code ci-dessous, mais mon application se bloque avec l'erreur comme hauteur et largeur supérieure à zéro.

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

Quel est le but réellement? La chose que vous essayez de faire est assez inhabituelle. –

+0

J'ai besoin de convertir mon image en document pdf pour mon application. –

Répondre

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

Merci pour la réponse, je vais essayer –

+0

Il vous donnera tout le contenu Scrollview en Bitmap. –

+0

il ne donne pas, il montre ne peut pas créer de vignette, et l'image vide –

0

utilisation ci-dessous le code pour convertir le contenu défilante à l'image.

//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: -

ScrollViewHelper public class étend ScrollView {

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

merci pour la réponse –

+0

J'ai utilisé scrollview, puis-je changer ma scrollview pour scrollViewHelper? –

+0

même erreur se produit, causée par: java.lang.IllegalArgumentException: la largeur et la hauteur doit être> 0 –