1

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?
BaRud
  • 3,055
  • 7
  • 41
  • 89

3 Answers3

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • ah..i thought so. But what is the utility of saving the bitmap then? is it viewable anyway? – BaRud Feb 22 '18 at 19:19
  • 1
    @BaRud: "But what is the utility of saving the bitmap then?" -- I do not know what you mean, sorry. "is it viewable anyway?" -- um, you can display a `Bitmap` in an `ImageView`, if that is what you mean. A `Bitmap` is simply an in-memory representation of a bitmap, just as `String` is an in-memory representation of text. – CommonsWare Feb 22 '18 at 19:20
  • In a different way, can I share the CardView directly using easyshare? – BaRud Feb 22 '18 at 19:22
  • @BaRud: I do not know what "easyshare" is, sorry. – CommonsWare Feb 22 '18 at 19:23
  • I meant this: https://developer.android.com/training/sharing/shareaction.html Thanks anyway. – BaRud Feb 22 '18 at 19:24
  • @BaRud: To use `ACTION_SEND`, you need to use a `content` `Uri` to pass things like images to the other app. A `content` `Uri` is backed by a `ContentProvider`. The simplest way for you to have such a `ContentProvider` is to save your `Bitmap` to a file, then use `FileProvider`. – CommonsWare Feb 22 '18 at 19:28
0

Here's my code working fine.I hope might be helpful this code

here xml fiel for cardView

<android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto"
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/card_view"
     android:layout_gravity="center"
     android:layout_width="250dp"
     android:layout_height="250dp"
     card_view:cardCornerRadius="4dp">

    <ImageView
        android:src="@drawable/img_"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.v7.widget.CardView>

Here code to download cardview as a jpeg file

find cardview CardView cardView = (CardView) findViewById(R.id.card_view); loadView(cardView);

Load view

 public void loadView(CardView cardView){

    try {
        cardView.setDrawingCacheEnabled(true);
        Bitmap bitmap =  loadBitmapFromView(cardView);
        cardView.setDrawingCacheEnabled(false);

        String mPath =
                Environment.getExternalStorageDirectory().toString() + "/sid.jpg";

        File imageFile = new File(mPath);
        FileOutputStream outputStream = new
                FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

    } catch (Throwable e) {
        e.printStackTrace();
    }
}

get Bitmap from view

 public Bitmap loadBitmapFromView(View v) {
    DisplayMetrics dm = getResources().getDisplayMetrics();
    v.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.EXACTLY));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
            v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(returnedBitmap);
    v.draw(c);

    return returnedBitmap;
}

cardview jpeg file save in ExternalStorage location with file name sid.jpg.

MyCompleted Activity class

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CardView cardView = (CardView) findViewById(R.id.card_view);
    loadView(cardView);
}
public void loadView(CardView cardView){

    try {
        cardView.setDrawingCacheEnabled(true);
        Bitmap bitmap =  loadBitmapFromView(cardView);
        cardView.setDrawingCacheEnabled(false);

        String mPath =
                Environment.getExternalStorageDirectory().toString() + "/sid.jpg";

        File imageFile = new File(mPath);
        FileOutputStream outputStream = new
                FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

    } catch (Throwable e) {
        e.printStackTrace();
    }
}

public Bitmap loadBitmapFromView(View v) {
    DisplayMetrics dm = getResources().getDisplayMetrics();
    v.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.EXACTLY));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
            v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(returnedBitmap);
    v.draw(c);

    return returnedBitmap;
}

}

Add premission on manifest file for save jpeg file

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Sagar Jethva
  • 986
  • 12
  • 26
0

You're passing the view object of the button. You've to inflate your layout into a view object then you've to pass that to your function. This might help you. Create a Bitmap from a Layout/View before/without draw

Salah
  • 101
  • 2
  • 2
  • 12
  • Even though a link is useful as a reference, please consider improving your answer by, for instance, using concrete examples that will help the OP or by extracting, from the link, the relevant contents. – António Ribeiro May 13 '20 at 14:14