简体   繁体   中英

how to convert string to bitmap image using scrollview in Android

I just want that whatever data in my editextbox i create the image of that data..may be it has 30 line or 40 or more then this and convert it into image and send that image to other activity.

///////////// XML FILE /////////////////

    <LinearLayout
    android:id="@+id/mainlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/line"
    android:layout_below="@+id/line2"
    android:background="#FFFFFF"
    android:gravity="center">

    <ScrollView
        android:id="@+id/scroll_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:cacheColorHint="@android:color/transparent"
        android:fadingEdge="none"
        android:fadingEdgeLength="0dp"
        android:gravity="center"
        android:scrollbarStyle="insideOverlay"
        android:scrollbars="none"
        android:fillViewport="true"
        android:scrollingCache="false">

        <EditText
            android:id="@+id/edt_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:cursorVisible="true"
            android:ems="10"
            android:gravity="center_vertical|center_horizontal"
            android:hint="..."
            android:inputType="textMultiLine"
            android:padding="3dp"
            android:paddingBottom="10dp"
            android:textSize="@dimen/font_size"
            android:windowSoftInputMode="stateHidden"
            android:maxLines="50">
            <requestFocus/>
        </EditText>
    </ScrollView>
</LinearLayout>

////////////////MAIN ACTIVITY . CLASS////////////////

    img_ok = (Button) findViewById(R.id.img_ok);
    img_ok.setOnClickListener(new OnClickListener() {

        @Override           
    public void onClick(View v) {           
        /*anstxt = edt_text.getText().toString();           
        butnlyt.setVisibility(View.GONE);       

        View view = findViewById(R.id.mainlayout);          
        view.refreshDrawableState();            
        view.setDrawingCacheEnabled(true);              
        view.buildDrawingCache();               
        view.buildDrawingCache();

        Bitmap bitmap = view.getDrawingCache();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();                 
        bitmap.compress(Bitmap.CompressFormat.PNG,100, stream);
        byte[] byteArray = stream.toByteArray();
        Intent in1 = new Intent(getApplicationContext(),PreviewActivity.class);     
        in1.putExtra("image", byteArray);
        startActivity(in1);         
        finish();*/ 

        edt_text.setCursorVisible(false);           
        edt_text.buildDrawingCache();               
        Bitmap bitmap = Bitmap.createBitmap(edt_text.getDrawingCache());            
        ByteArrayOutputStream stream = new ByteArrayOutputStream();         
        bitmap.compress(Bitmap.CompressFormat.PNG,100, stream);         
        byte[] byteArray = stream.toByteArray();

        Intent in1 = new Intent(getApplicationContext(),PreviewActivity.class);         
        in1.putExtra("image", byteArray);           
        startActivity(in1);
        finish();               
    }

});

Here is the procedure for converting string to bitmap but string should Base64 encoding

  /**
   * @param encodedString
   * @return bitmap (from given string)
   */
  public Bitmap StringToBitMap(String encodedString){
 try{
   byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
   Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
   return bitmap;
 }catch(Exception e){
   e.getMessage();
   return null;
 }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM