将卡片视图转换为位图 [英] convert a cardview to bitmap
问题描述
类似类型的问题已被问过多次,但我仍然很难理解图像的保存位置.我正在使用this SO question的公认解决方案.
similar type of questions has been asked several times, but I am still having a tough time to understand where the image is saved. I am using the accepted solution of this SO question.
我有一个 cardview
,我想将其转换为图像并共享(这是一个不同的问题).我的卡片视图是:
I have a cardview
that I want to convert to an image and share it(that's a different issue).
My cardview is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
......
android.support.v7.widget.CardView
android:id="@+id/cv_abtme"
android:layout_width="368sp"
android:layout_height="273dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="101dp"
android:background="@color/colorPrimaryLight"
app:cardBackgroundColor="@color/about_instagram_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:srcCompat="@mipmap/ic_launcher" />
....
</android.support.v7.widget.CardView>
我正在尝试将其转换为:
I am trying to convert it as:
public class AboutMeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_me);
ImageButton cvbutton= findViewById(R.id.imageButton_abtme);
cvbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getBitmapFromView(view);
Snackbar.make(getCurrentFocus(),"Image Captured", Snackbar.LENGTH_LONG).show();
}
});
}
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
}
但我不知道它是如何工作的,因为我看不到任何生成的图像,但显然没有错误.
But I have no idea how it's working, as I can't see any image produced, but obviously no error.
所以,问题是:
- 创建图像的路径是什么?
- 我是否需要一些权限才能保存和访问 jpg?
推荐答案
图片创建的路径是什么?
What is the path the image created?
根据您问题中的代码,它没有保存在任何地方.您没有编写任何代码来保存它.您创建了一个 Bitmap
对象,就是这样.
Based on the code in your question, it is not saved anywhere. You did not write any code to save it. You created a Bitmap
object, and that is it.
要将 Bitmap
保存为磁盘上的图像文件,请调用 compress()
,指定图像类型和 FileOutputStream
到您所在的位置想保存它.
To save a Bitmap
as an image file on disk, call compress()
, specifying the image type and a FileOutputStream
to where you want to save it.
这篇关于将卡片视图转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!