//참고로 setWallPaper는 권한으로 인해 예외가 남..주석처리 하세영..

//100프로 실행됨..최고의 소스...2틀동안 고생하다 발견한 주옥같은 소스..T.T 난 바보양..


/ 소스는 밑에 굵게 표시된 부분임..

 

 

< How to save a canvas to disk Options >


http://groups.google.com/group/android-developers/browse_thread/thread/6aeed20525760f3/46407ecb1c68502e?hl=en&lnk=gst&q=canvas+to+file#46407ecb1c68502


Hi, 
I am doing a painting program (KIds Paint - you can find in Android 
Market) and I have a lot of requests to save the content on disk or to 
wallpaper. I have been searching around but cannot find solution. 
My guess is that I probably wanted to get the bitmap from the canvas, 
but I can't find ways to get it. Then I try to set an empty bitmap 
into the canvas and draw on the canvas, and save the bitmap... but I 
got an empty bitmap. 
Please help! Thanks. Here's my codes: 
public class KidsPaintView extends View { 
        Bitmap bitmap = null; 
... 
 protected void onDraw(Canvas canvas) { 
        if (bitmap == null) { 
            bitmap = Bitmap.createBitmap(width, height, 
Bitmap.Config.ARGB_8888); 
            canvas.setBitmap(bitmap); 
        } 
  ... // do painting on canvas 
  } 
}

Then in my main code I try to retrieve the bitmap and save it as 
wallpaper: 
Bitmap bitmap = view.bitmap; 
try { setWallpaper(bitmap); } 
catch (IOException e) { e.printStackTrace(); } 
But all I got is a black wallpaper.

    Reply to author     Forward        Report spam     Rate this post:  
  
gjs   
View profile  
 More options Sep 23 2009, 3:23 pm
Hi, 
Have a look at - 
http://developer.android.com/reference/android/view/View.html#draw(an...) 
- create a new bitmap and a new canvas, associate the bitmap with this 
canvas and call view.draw( your_new_canvas ) - using your 
KidsPaintView view instance - then save the bitmap or use it to set 
the wallpaper. 
Regards 
On Sep 23, 1:03 pm, limtc <thyech...@gmail.com> wrote:

- Show quoted text -

    Reply to author     Forward        Report spam     Rate this post:  
  
limtc   
View profile  
 More options Sep 23 2009, 5:12 pm
Mmm... I don't quite understand. The link you sent is the 
documentation for View? 
If I have a new canvas, what happened to the canvas in onDraw? And 
when should I call view.draw(canvas)? 
Do you have any sample codes? Appreciated.


    Reply to author     Forward        Report spam     Rate this post:  
  
gjs   
View profile  
 More options Sep 24 2009, 9:12 pm
Hi, 
I made up the following to demonstrate what I meant. 
In this example when you press the trackball/dpad center button the 
view is written to a png file and also set as the wallpaper. 
You can change it to save as jpeg file etc. 
Hope that helps. 
Regards 
///////////////////////////////////////////// 
package com.testSaveView; 
import android.app.Activity; 
import android.os.Bundle; 
import java.io.FileOutputStream; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
public class testSaveView extends Activity 

        SomeView sv = null; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        sv = new SomeView(this); 
        setContentView( sv ); 
    } 
    @Override 
    public boolean onKeyDown( int keyCode, KeyEvent event) 
        { 
        switch( event.getKeyCode() ) 
                { 
                case KeyEvent.KEYCODE_DPAD_CENTER: 
                        if ( sv != null ) 
                        { 
                                saveView( sv ); 
                                return true; 
                        } 
                default: 
                } 
                return super.onKeyDown( keyCode, event ); 
        } 
    private void saveView( View view ) 
    { 
       Bitmap  b = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); 
       Canvas c = new Canvas( b ); 
       view.draw( c ); 
       FileOutputStream fos = null; 
       try { 
                fos = new FileOutputStream( "/sdcard/some_view_image_" + System.currentTimeMillis() + ".png" ); 
                if ( fos != null ) 
                { 
                        b.compress(Bitmap.CompressFormat.PNG, 100, fos ); 
                        fos.close(); 
                } 
                setWallpaper( b ); 
            } catch( Exception e ) 
                        { 
                        Log.e("testSaveView", "Exception: " + e.toString() ); 
                        } 
    } 
class SomeView extends View 

        public SomeView( Context context ) 
        { 
          super( context ); 
        } 
        public void onDraw( Canvas canvas ) 
        { 
                canvas.drawARGB(0x80, 0xff, 0, 0 ); 
                Paint paint = new Paint(); 
                paint.setColor(Color.BLUE); 
                paint.setTextSize( 48 ); 
                canvas.drawText("...Some view...", 10, canvas.getHeight() / 2, 
paint); 
        } 

}

////////////////////////// 
& the manifest - 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android
      package="com.testSaveView" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/ 
app_name"> 
        <activity android:name=".testSaveView" 
                  android:label="@string/app_name"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category 
android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
    </application> 
    <uses-sdk android:minSdkVersion="3" /> 
<uses-permission android:name="android.permission.SET_WALLPAPER"></ 
uses-permission> 
</manifest> 
On Sep 23, 7:12 pm, limtc <thyech...@gmail.com> wrote:

- Show quoted text -

    Reply to author     Forward        Report spam     Rate this post:  
  
limtc   
View profile  
 More options Sep 24 2009, 10:45 pm
Thanks! 
I think the part that I am confused is that I never used view.draw 
(canvas) code in my program so I don't know when to call it... at this 
moment, says I wanted to clear a screen, this is my code in the view: 
    public void clearScreen() { 
        gradient = false; 
        dots.clear(); 
        invalidate(); // this will call the onDraw and pass in the 
canvas 
    } 
so I replace invalidate() by draw(canvas)?


    Reply to author     Forward        Report spam     Rate this post:  
  
gjs   
View profile  
 More options Sep 25 2009, 3:39 pm
Hi, 
That is why I gave you a complete working example ( maybe try it 
yourself ). But not knowing your code I could only guess about when 
you would want to call the saveView() method I provided. 
I guess that you would call the saveView() method when the user 
requests that your KidsPaintView view should save their 'drawing' 
either to a file or to set as the wallpaper. 
I imagine that you would provide a menu option button, or some 
touchscreen or keyboard button to allow the user to trigger that 
action - when they had finished making their 'drawing'. I just used 
the trackball/dpad center button as an example to demonstrate 
triggering and saving the view. 
In my example I used 'SomeView' to represent your KidsPaintView and 
put the saveView() method in the Activity, but you could also put the 
saveView() method or some equivalent code inside your KidsPaintView 
view if you wish. 
You should also realize that you CAN just call draw( canvas ) on your 
KidsPaintView as your class extends View so it (automatically) 
inherits the View.draw( canvas ) method... You just need to ensure 
that you create and use a new canvas and a new bitmap when calling the 
draw( canvas) method, as I said originally and demonstrated with the 
code posted. 
Good luck ! 
Regards 
On Sep 25, 12:45 am, limtc <thyech...@gmail.com> wrote:

- Show quoted text -

    Reply to author     Forward        Report spam     Rate this post:  
  
limtc   
View profile  
 More options Sep 29 2009, 4:03 pm
Thanks! 
After experiementing, seems to work. I think all I need to do is to do 
the view.draw(canvas) prior to have the canvas filled with what's 
drawn on screen. I am still exactly sure what's happening though, as 
basically I just do everything as it is, and create a new bitmap and a 
new canvas associate with it, and prior to saving, pass the new canvas 
to the view. 
About view.draw(canvas): 
http://developer.android.com/reference/android/view/View.html#draw(an...) 
Anyway, I am happy! 

+ Recent posts