保存画布后,图片不可用

时间:2012-06-10 10:42:12

标签: android canvas

我试图将画布的状态保存到我的SD卡或手机画廊,这对我来说并不重要。我在下面有这些代码,当点击屏幕时,文件会保存在SD卡中但作为无法识别的文件,就像感叹号警告标记图片不可用

代码:

package your.save.namespace; 
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.MotionEvent;
import android.view.View; 
import android.widget.Toast;
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 onTouchEvent(MotionEvent event) 
    {
     int type=event.getAction();
     Log.i(null,"GOOOD");
     if(type==MotionEvent.ACTION_DOWN)
        { 

                        if ( sv != null ) 
                        { 
                              Log.i(null,"GOOOD");
                                saveView( sv ); 
                                return true; 

                        } 
                        Log.i(null,"GOOOD");
        }
     return true;
     }
    private void saveView( View view ) 
    { 
         Log.i(null,"GOOOD");
         Log.i(null,"hii"+view.getWidth()+"hello"+view.getHeight ());
       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" ); 
                Toast.makeText( null, "Saved", Toast.LENGTH_LONG).show();
                Log.i(null,"GOOOD");

                if ( fos != null ) 
                { 
                    Log.i(null,"GOOOD!!!!!!!");

                        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); 
        } 
} 
} 

问题可能在于清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.save.namespace"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        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> 

1 个答案:

答案 0 :(得分:0)

<uses-sdk > repeat 2 times 3 & 10 remove any 1

要访问SD卡,您需要使用权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />