キャンバスの状態をSDカードまたは電話ギャラリーに保存しようとしていますが、それは私には関係ありません。以下にこれらのコードを示します。画面をクリックすると、ファイルがSDカードに保存されますが、感嘆符の警告マークのように、認識されないファイルとしてpicが利用できません
コード:
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>