问题
I want to convert textView into Bitmap but I am getting blackscreen Instead of the data inside textview.
Screen I want to convert
Screen which I am getting after drawing cache
I don't know where I am going wrong.
Following is my code
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();
}
Thanks in advance..
回答1:
Transparent background usually causes the background to get black when converted into bitmap, try putting background color of your background frame of TextView
to somewhat like white color then try converting it to bitmap.
回答2:
Try creating bitmap from TextView, currently you are converting RelativeLayout to Bitmap...
来源:https://stackoverflow.com/questions/38850809/solve-black-screen-while-converting-textview-into-bitmap