Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
925 views
in Technique[技术] by (71.8m points)

android - convert a cardview to bitmap

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.

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.

So, the question is:

  1. What is the path the image created?
  2. Do I need some permission to save and access the jpg?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What is the path the image created?

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...