Convert cartography to bitmap
a similar type of question has been asked several times, but I'm still having a hard time finding where to save the image. I am using the accepted solution of this SO question .
I have cardview
one that I want to convert to an image and share (that's another problem). My map view:
<?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 like:
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 works as I can't see any image, but obviously no error.
So the question is:
- What is the way to create an image?
- Do I need permission to save and access the jpg?
source to share
What is the way to create an image?
Based on the code in your question, it is not being saved anywhere. You haven't written any code to save it. You have created an object Bitmap
and that's it.
To save Bitmap
as an image file to disk, call compress()
by specifying the type of image and FileOutputStream
where you want to save it.
source to share
Here my code works fine. Hope this code can be helpful
here xml
fiel forcardView
<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's the code to load cardview as file jpeg
find card CardView cardView = (CardView) findViewById (R.id.card_view); loadView (cardView);
Download performance
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;
}
Save jpeg file in ExternalStorage format with filename 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 to file manifest
to save jpeg file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
source to share