【问题标题】:Can we save canvas as an Image to sd card in Android?我们可以将画布作为图像保存到 Android 中的 sd 卡吗?
【发布时间】:2017-06-29 13:09:53
【问题描述】:

我在 Android Studio 中使用 Canvas,在画布上有 .png 图像,并希望将其保存到 sd 卡。是否可以? 如果是,那怎么办?

感谢和问候。

【问题讨论】:

标签: android android-canvas


【解决方案1】:

此代码可能会对您有所帮助 (Saving canvas to bitmap on Android)

Bitmap toDisk = null;
        try {

            // TODO: Get the size of the canvas, replace the 640, 480
            toDisk = Bitmap.createBitmap(640,480,Bitmap.Config.ARGB_8888);
            canvas.setBitmap(toDisk);
            toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("arun.jpg")));

        } catch (Exception ex) {


        }

【讨论】:

    【解决方案2】:

    您应该已经使用 new Canvas(myBitmap); 创建了 Canvas。因此,当您在 Canvas 上绘图时,它会绘制到您的位图。

    String fileName = Environment.getExternalStorageDirectory() + "/test.png";
    OutputStream stream = new FileOutputStream(fileName);
    /* Write bitmap to file using JPEG or PNG and 80% quality hint for JPEG. */
    myBitmap.compress(CompressFormat.PNG, 80, stream);
    stream.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多