【问题标题】:Android studio - how can I save a textView's text as a PNG image (with it's font, color, and font size)? [duplicate]Android Studio - 如何将 textView 的文本保存为 PNG 图像(使用它的字体、颜色和字体大小)? [复制]
【发布时间】:2015-10-02 10:08:04
【问题描述】:
我在互联网上研究了很多,但没有用。
我对 Android 编程和 java 非常陌生。
我在 Android Studio 中编写了一个简单的程序,可以让你用我设置的很酷的字体编写文本,用户可以选择颜色和字体大小。
我想保存这个文本,包括它的字体、字体大小和字体颜色;作为任何厨房应用程序都可以查看的图像。我尝试了很多事情,花了几个小时,但宇宙仍然反对我。
我设法创建了一个位图图像,我可以在 ImageView 中查看它,但我无法将它保存到存储中。
有人可以简单地解释一下吗?我对 Android 和 Java 都很陌生。
【问题讨论】:
-
您所需要的只是将视图(在本例中为您的 TextView)内容绘制到位图,并保存到存储中。试试this PS:saveImageToInternalStorage 取自下面的答案。
标签:
java
android
image
bitmap
storage
【解决方案1】:
public boolean saveImageToInternalStorage(Bitmap image) {
try {
// Use the compress method on the Bitmap object to write image to
// the OutputStream
FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
// Writing the bitmap to the output stream
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
return true;
}
catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
return false;
}
}