android - 解决textview转位图时黑屏

标签 android bitmap textview screenshot

我想将 textView 转换为位图,但我得到的是黑屏而不是 textview 中的数据。

我要转换的屏幕

enter image description here

我在绘制缓存后得到的屏幕

enter image description here

我不知道我哪里错了。

以下是我的代码

abc.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
android:paddingBottom="5dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.bgc.activity.AlterQuotes"
tools:showIn="@layout/activity_alter_quotes">

<RelativeLayout
    android:id="@+id/AlterQuotesrl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.18">

    <TextView
        android:id="@+id/AlterQuotestvTestText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="15sp"
        android:textColor="#000000"
        android:background="@drawable/frame"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/sample_name"/>

</RelativeLayout>

</LinearLayout>

abc.java

RelativeLayout z = (RelativeLayout) findViewById(R.id.AlterQuotesrl);
z.setDrawingCacheEnabled(true);
z.buildDrawingCache();
Bitmap bitmapSetDrawingChache = Bitmap.createBitmap(z.getDrawingCache());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmapSetDrawingChache.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

// saving in sdcard
File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "Birthday Card" + File.separator);
if(!file.exists())
    file.mkdirs();

String imageName = "Image-" + DateFormat.getDateTimeInstance().format(new Date()) + ".jpg";

fileImagePath = new File(file,imageName);
try {
    FileOutputStream fileOutputStream = new FileOutputStream(fileImagePath);
    fileOutputStream.write(byteArrayOutputStream.toByteArray());
    fileOutputStream.close();

    AlertDialog.Builder adb = new AlertDialog.Builder(AlterQuotes.this);
    adb.setTitle("Alert");
    adb.setMessage("You will not be able to change, are you sure you want to continue?");
    adb.setIcon(this.getResources().getDrawable(R.drawable.ic_black_alert_two));
    adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) { 
            Intent intent = new Intent(AlterQuotes.this, CardMaker.class);
            intent.putExtra("bitmapPath",fileImagePath.toString());
            startActivity(intent);
        }
    });
    adb.show();
} catch (IOException e) {
    e.printStackTrace();
  }

提前致谢..

最佳答案

透明背景通常会导致转换为位图时背景变黑,请尝试将 TextView 背景框架的背景颜色设置为有点像白色,然后再尝试将其转换为位图。

关于android - 解决textview转位图时黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38850809/

相关文章:

java - 安卓.view.InflateException : Binary XML file line #10: Error inflating class fragment

android - 分割和重新加入位图

java - 如何在 TextView 中显示 ArrayList 中的字符串

Android:使用主题/样式更改禁用文本的颜色?

java - 插入错误且未找到表

Android,访问内部声音文件作为资源 ID

安卓 : Maximum allowed width & height of bitmap

java - Android 尝试旋转图像时出现 OutOfMemoryError

java - 在 android 中设置 TextView 的可见性不起作用

android - 如何在 Jenkins 中批准 Android SDK 工具的许可