gpt4 book ai didi

android - 在 Canvas 上绘制图像并保存到 sd 卡中

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:46 24 4
gpt4 key购买 nike

我尝试使用下面的代码来

  1. 在 Canvas 上作画
  2. 在图像上保存 Canvas

问题 - 当我尝试保存图像时,它显示类似权限被拒绝的错误。我的错误日志如下。

在 Canvas 上绘制的代码:

public MyDrawView(Context c, AttributeSet attrs) {
super(c, attrs);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFF000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(9);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}

public Bitmap getBitmap() {
this.setDrawingCacheEnabled(true);
this.buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap(this.getDrawingCache());
this.setDrawingCacheEnabled(false);
return bmp;
}

public void clear(){
mBitmap.eraseColor(Color.GREEN);
invalidate();
System.gc();
}

}

将 Canvas 另存为图像的第二个代码在主 Activity 中。由于我是初学者,我很感激任何建议。

第二个代码MainActivity:

public class MainActivity extends Activity {
MyDrawView myDrawView;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDrawView = (MyDrawView) findViewById(R.id.draw);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

File folder = new File(Environment
.getExternalStorageDirectory().getAbsolutePath()+"/Folder image");
if (!folder.exists()) {
folder.mkdirs();
}

File file = new File(folder,"sample.png");

if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}


FileOutputStream ostream = null;
try {
ostream = new FileOutputStream(file);

System.out.println(ostream);
View targetView = myDrawView;

Bitmap well = myDrawView.getBitmap();
Bitmap save = Bitmap.createBitmap(320, 480, Config.ARGB_8888);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
Canvas now = new Canvas(save);
now.drawRect(new Rect(0, 0, 320, 480), paint);
now.drawBitmap(well, new Rect(0, 0, well.getWidth(), well.getHeight()), new Rect(0, 0, 320, 480), null);

if (save == null) {
System.out.println("NULL bitmap save\n");
}
save.compress(Bitmap.CompressFormat.PNG, 100, ostream);
} catch (NullPointerException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Null error", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "File error", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "IO error", Toast.LENGTH_SHORT).show();
}

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

我的错误日志是:

12-16 13:12:13.848: W/System.err(2010): java.io.IOException: open failed: EACCES (Permission denied)
12-16 13:12:13.848: W/System.err(2010): at java.io.File.createNewFile(File.java:940)
12-16 13:12:13.848: W/System.err(2010): at com.example.drawimage.MainActivity$1.onClick(MainActivity.java:48)
12-16 13:12:13.848: W/System.err(2010): at android.view.View.performClick(View.java:4084)
12-16 13:12:13.848: W/System.err(2010): at android.view.View$PerformClick.run(View.java:16966)
12-16 13:12:13.848: W/System.err(2010): at android.os.Handler.handleCallback(Handler.java:615)
12-16 13:12:13.848: W/System.err(2010): at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 13:12:13.848: W/System.err(2010): at android.os.Looper.loop(Looper.java:137)
12-16 13:12:13.848: W/System.err(2010): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-16 13:12:13.848: W/System.err(2010): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 13:12:13.848: W/System.err(2010): at java.lang.reflect.Method.invoke(Method.java:511)
12-16 13:12:13.848: W/System.err(2010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-16 13:12:13.848: W/System.err(2010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-16 13:12:13.848: W/System.err(2010): at dalvik.system.NativeStart.main(Native Method)
12-16 13:12:13.848: W/System.err(2010): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
12-16 13:12:13.848: W/System.err(2010): at libcore.io.Posix.open(Native Method)
12-16 13:12:13.848: W/System.err(2010): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
12-16 13:12:13.848: W/System.err(2010): at java.io.File.createNewFile(File.java:933)
12-16 13:12:13.848: W/System.err(2010): ... 12 more
12-16 13:12:13.848: W/System.err(2010): java.io.FileNotFoundException: /mnt/sdcard/Folder image/sample.png: open failed: EACCES (Permission denied)
12-16 13:12:13.848: W/System.err(2010): at libcore.io.IoBridge.open(IoBridge.java:416)
12-16 13:12:13.848: W/System.err(2010): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
12-16 13:12:13.848: W/System.err(2010): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
12-16 13:12:13.848: W/System.err(2010): at com.example.drawimage.MainActivity$1.onClick(MainActivity.java:58)
12-16 13:12:13.848: W/System.err(2010): at android.view.View.performClick(View.java:4084)
12-16 13:12:13.848: W/System.err(2010): at android.view.View$PerformClick.run(View.java:16966)
12-16 13:12:13.848: W/System.err(2010): at android.os.Handler.handleCallback(Handler.java:615)
12-16 13:12:13.848: W/System.err(2010): at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 13:12:13.848: W/System.err(2010): at android.os.Looper.loop(Looper.java:137)
12-16 13:12:13.848: W/System.err(2010): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-16 13:12:13.848: W/System.err(2010): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 13:12:13.848: W/System.err(2010): at java.lang.reflect.Method.invoke(Method.java:511)
12-16 13:12:13.848: W/System.err(2010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-16 13:12:13.848: W/System.err(2010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-16 13:12:13.848: W/System.err(2010): at dalvik.system.NativeStart.main(Native Method)
12-16 13:12:13.848: W/System.err(2010): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
12-16 13:12:13.848: W/System.err(2010): at libcore.io.Posix.open(Native Method)
12-16 13:12:13.848: W/System.err(2010): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
12-16 13:12:13.848: W/System.err(2010): at libcore.io.IoBridge.open(IoBridge.java:400)
12-16 13:12:13.848: W/System.err(2010): ... 14 more

AndroidManifest.xml:

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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>

</manifest>

最佳答案

您不应该将文件夹命名为“/Folder image”,删除空格就可以了。

关于android - 在 Canvas 上绘制图像并保存到 sd 卡中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com