4

I have a drawing application and I have methods for saving pictures drawn. But I only get a black bitmap picture with the save button. Where is the problem ?

There is my xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/all"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".Circle" 
   >

<org.vkedco.mobappdev.draw_touch_drive_00001.krouzky
    android:id="@+id/pntr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:tag="Painter" />



 <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/button4"
    android:layout_marginTop="50dp"
    android:text="value"
    android:textColor="@color/white" />


<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/button2"
    android:text="Add" />


<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Delete" />

  <Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Save"
    android:layout_marginTop="45dp"
    android:layout_alignLeft="@+id/button3" />


  <Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Clear"
    android:layout_marginTop="45dp"
    android:layout_alignRight="@+id/button3" />

 <LinearLayout 
     android:orientation="vertical"
     android:layout_height="match_parent"
      android:id="@+id/linearLayout"
     android:layout_width="match_parent" />

And my main Activity:

public class aktivita extends Activity{
Button btn;
public LinearLayout mContent;
krouzky mTicTacToeView = null;
public static String tempDir;
public File mypath;
 public static Bitmap mBitmap;
public static int width;
public static int height;
public static float x;
public static float y;
public static float X;
public static float Y;
public static double vzdalenost;
 public String current = null;
 private String uniqueId;
 public krouzky mSignature;
 public View mView;




public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.zaznam_ran);      
   krouzky.t = 0;






   File directory = new File(Environment.getExternalStorageDirectory() + "/Images");
   if(!directory.exists())

       directory.mkdir();

   uniqueId = getTodaysDate() + "_" + getCurrentTime();
   current = uniqueId + ".png";


   mypath= new File(directory,current);

   mContent = (LinearLayout) findViewById(R.id.linearLayout);

   mSignature = new krouzky(this, null);
   mSignature.setBackgroundColor(Color.TRANSPARENT);
   mContent.addView(mSignature, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

   mView = mContent;





    Display display = getWindowManager().getDefaultDisplay();
     width = (display.getWidth());
     height = (display.getHeight());        
    mTicTacToeView = (krouzky) this.findViewById(R.id.pntr);





     Button btn6 = (Button) findViewById(R.id.button6);

        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    Log.v("log_tag", "Panel Saved");

                        mView.setDrawingCacheEnabled(true);
                       save(v);





                }


        });

}
     private boolean prepareDirectory()
    {
        try
        {
            if (makedirs())
            {
                return true;
            }
            else 
            {
                return false;
            }
        } 
        catch (Exception e)
        {
            e.printStackTrace();
            Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", 1000).show();
            return false;
        }
    }

    private boolean makedirs()  
    {
        File tempdir = new File(tempDir);
        if (!tempdir.exists())
            tempdir.mkdirs();

        if (tempdir.isDirectory())
        {
            File[] files = tempdir.listFiles();
            for (File file : files)
            {
                if (!file.delete())
                {
                    System.out.println("Failed to delete " + file);
                }
            }
        }
        return (tempdir.isDirectory());
    }   

method for saving

    public  void save(View v)
    {
        Log.v("log_tag", "Width: " + v.getWidth());
        Log.v("log_tag", "Height: " + v.getHeight());
        if(mBitmap == null)
        {
            mBitmap =  Bitmap.createBitmap (width, height, Bitmap.Config.RGB_565);
        }
        Canvas canvas = new Canvas(mBitmap);
        try
        {

            FileOutputStream mFileOutStream = new FileOutputStream(mypath);

            v.draw(canvas);
            mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
            mFileOutStream.flush();
            mFileOutStream.close();


        }
        catch(Exception e)
        {
            Log.v("log_tag", e.toString());
        }
    }

My problem, was resolved, but I have one small detail. My application get down, when I change my xml. to this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".Circle" >

<org.vkedco.mobappdev.draw_touch_drive_00001.krouzky
    android:id="@+id/pntr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:tag="Painter" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/button2"
    android:text="Add" />


<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Delete" />

  <Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Save"
    android:layout_marginTop="45dp"
    android:layout_alignLeft="@+id/button3" />


  <Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Clear"
    android:layout_marginTop="45dp"
    android:layout_alignRight="@+id/button3" />

<LinearLayout 
     android:orientation="vertical"
     android:layout_height="match_parent"
      android:id="@+id/linearLayout"
     android:layout_width="match_parent" >

</LinearLayout>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button6"
    android:layout_marginTop="21dp"
    android:text="value"
    android:textColor="@color/blue" />

Josef Josef
  • 65
  • 2
  • 9
  • possible duplicate of [Android 2.1 View's getDrawingCache() method always returns null](http://stackoverflow.com/questions/2817166/android-2-1-views-getdrawingcache-method-always-returns-null) – dimetil Aug 28 '13 at 13:44

4 Answers4

4

Try this: - find your parent view

RelativeLayout holder = (RelativeLayout) findViewById(R.id.all);

- And call loadBitmapFromView(holder).

public static Bitmap loadBitmapFromView(View v) {
    DisplayMetrics dm = getResources().getDisplayMetrics(); 
    v.measure(MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.EXACTLY));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    ((RelativeLayout) v).setGravity(Gravity.CENTER);
    Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
            v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(returnedBitmap);
    v.draw(c);

    return returnedBitmap;
}
validcat
  • 6,158
  • 2
  • 29
  • 38
2

Actually You are passing the view (v) of your Button btn6 in method save(v)- Pass linear layout object which is mcontent or mView, then that would be save(mContent), This will solve your problem

You can try this method also-

Pass your parent layout or view in method-

 Bitmap file = save(mcontent);

 Bitmap save(View v)
   {
    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.draw(c);
    return b;
   }
T_V
  • 17,440
  • 6
  • 36
  • 48
  • Wow thank you very much. So small importaint detail. Thank you. – Josef Josef Aug 28 '13 at 17:38
  • And, one small thing. Can anyone say me, why my aplication get down. If I move TextView in xml file under LinearLayout (for saving drawn image with text in TextView) ? – Josef Josef Aug 28 '13 at 20:56
0

You can use View.getDrawingCache()

Before it you should call View.setDrawingCache(true);

Changwei Yao
  • 13,051
  • 3
  • 25
  • 22
0

I've created a cutom component that extends GroupView and used this method to save this layout to a bitmap.

    public Bitmap createBitmap() {
    //Log.d("Pin", "Image W ["+this.getWidth()+"] x H ["+this.getHeight()+"]");
    Bitmap b = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.RGB_565);       
    Canvas c = new Canvas(b);

    this.draw(c);

    return b;
}

Hope it helps you. If you want to save your layout you should change this with the entire layout you want to save

FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22