• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Save LinearLayout as image after finger painting on it

nice_bro

Lurker
Hi,
I am new to android, and creating a small application where I want to get an image from gallery and then after drawing on it (using finger paint) save it to gallery.

The problem I am having is after drawing, when I try to save the image it only saves the drawing with black background. The image taken from the gallery is not visible in saved image.

XML Layout:

HTML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFCCCCCC"
android:orientation="vertical"
tools:context=".ImageActivity" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:orientation="horizontal" >
    <ImageButton
        android:id="@+id/my_save_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/save"
        android:src="@drawable/save" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:id="@+id/my_view_drawing_pad1"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1" >

    <LinearLayout
        android:id="@+id/my_view_drawing_pad"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>
</LinearLayout>

ImageActivity class:

HTML:
public class ImageActivity extends Activity implements OnClickListener
{
   private DrawingView drawView;
   private ImageButton ibsaveBtn;
   LinearLayout llDrawingPad;

   @Override
   protected void onCreate(Bundle savedInstanceState) 
   {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_image);
     drawView = new DrawingView(this);
     llDrawingPad = (LinearLayout) findViewById(R.id.my_view_drawing_pad);
     ibsaveBtn = (ImageButton)findViewById(R.id.my_save_btn);
     ibsaveBtn.setOnClickListener(this);

    // code to get image from gallery ...

   }

   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) 
   {
     // getting the image

     File file = new File(s);
     if (file.exists()) {

         fp = file.getAbsolutePath();
         d = Drawable.createFromPath(file.getAbsolutePath());

         drawView = new DrawingView(this);
         llDrawingPad = (LinearLayout) findViewById(R.id.my_view_drawing_pad);
         llDrawingPad.addView(drawView);
         llDrawingPad.setBackgroundDrawable(d);
      }
   } // end onActivityResult

   @Override
   public void onClick(View view) 
   {
      drawView.setDrawingCacheEnabled(true);

      String imgSaved = MediaStore.Images.Media.insertImage(
            getContentResolver(), drawView.getDrawingCache(), UUID.randomUUID()
            .toString() + ".png", "drawing");

      if (imgSaved != null) 
      {
         Toast savedToast = Toast.makeText(getApplicationContext(),
                "Drawing saved to Gallery!", Toast.LENGTH_SHORT);
         savedToast.show();
      } else {
               Toast unsavedToast = Toast.makeText(getApplicationContext(),
                "Oops! Image could not be saved.", Toast.LENGTH_SHORT);

               unsavedToast.show();
             }
      drawView.destroyDrawingCache();
   } // end onClick

Could you please tell me, how to save the image with drawing on it.
What am I missing here?
 
Back
Top Bottom