我有cardview
我要转换为图片并分享它(这是一个不同的问题)。
我的cardview是:
<?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>
我正在尝试将其转换为:
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;
}
}
但我不知道它是如何工作的,因为我无法看到任何图像产生,但显然没有错误。
所以,问题是:
答案 0 :(得分:1)
图片创建的路径是什么?
根据您问题中的代码,它不会保存在任何地方。您没有编写任何代码来保存它。您创建了一个Bitmap
对象,就是这样。
要将Bitmap
保存为磁盘上的图像文件,请拨打compress()
,指定图片类型,然后FileOutputStream
到您要保存的位置。
答案 1 :(得分:0)
这是我的代码正常工作。我希望这段代码可能会有所帮助
这里xml
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>
此处代码将cardview下载为jpeg
文件
加载视图
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;
}
cardview jpeg文件保存在ExternalStorage位置,文件名为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;
}
}
在manifest
文件上添加预存以保存jpeg文件
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
答案 2 :(得分:0)
您正在传递按钮的视图对象。 您必须将布局膨胀到视图对象中,然后才能将其传递给函数。 这可能对您有帮助。 Create a Bitmap from a Layout/View before/without draw