将整个布局转换为 image/bitmap
Converting an entire layout to an image/bitmap
我对这一行代码的问题已经有一段时间了,如果你们中有人对此有想法,我将不胜感激。
我想做的是,当有人点击按钮时,将特定布局(在本例中为相对布局)保存为图像。此操作会将图像 运行 转移到下一个 activity。我很确定 t运行sfering 的实际行为不是问题所在。这就是我将布局转换为图像的方式。下面是带有注释的代码:
这是 activity1
上的内容
// here I found the button I wanted to use and made it:
// 1. take us to next activity
// 2. convert my relativelayout to and a bitmap and store it in a variable for transfer.
findViewById<Button>(R.id.DoneButton).setOnClickListener{
val intent = Intent(this, SaveAndNameActivity::class.java)
//here is where I converted my relative layout to a bitmap.
val pictureStackCompleted = findViewById<RelativeLayout>(R.id.pictureStack).drawToBitmap()
//here is where I prepared it for transfer to the next activity (NOTE!: the value here is Parceable? , I chose this bcause it works with bitmaps)
intent.putExtra("pictureStackCompleted" , pictureStackCompleted)
//here is where I told activity2 to start
startActivity(intent)
}```
这是 Activity2 上的内容
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_save_and_name)
//here is where I called the variable containing, my relative layout turned bitmap image
val completedPicture = intent.getParcelableExtra<Bitmap>("pictureStackCompleted")
//here I set my image to be the background image of an ImageView on activity2
val finalImage = findViewById<ImageView>(R.id.finalImage)
finalImage.setImageBitmap(completedPicture)
}
我 100% 确定它与位图转换行有关,因为我测试了那行代码,将它从意图中移到应用程序的另一部分(工作正常)。当我 运行 应用程序的工作部分不再运行时。
原因是您试图使用 Intent
传递 Bitmap
。尽管理论上可行,但 Bitmap
的大小会导致问题,如您的错误所示:
E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 3602192)
如果您想将 Bitmap
传递给另一个 activity,那么我建议:
将 Bitmap
保存到文件
将这些添加到您的 manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
那么下面的代码应该可以工作
File bitmapFile = new File(Environment.getExternalStorageDirectory() + "MyBitmap" + ".png");
FileOutputStream fos = new FileOutputStream(bitmapFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 80, fos);
fos.flush();
fos.close();
使用 Intent
/Bundle
将文件路径作为 String
发送到下一个 activity
intent.putExtra("filename", "MyBitmap.png");
使用下一个activity中的文件路径重新创建Bitmap
String fileName = getIntent().getStringExtra("filename");
String filePath = Environment.getExternalStorageDirectory() + fileName;
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
我对这一行代码的问题已经有一段时间了,如果你们中有人对此有想法,我将不胜感激。
我想做的是,当有人点击按钮时,将特定布局(在本例中为相对布局)保存为图像。此操作会将图像 运行 转移到下一个 activity。我很确定 t运行sfering 的实际行为不是问题所在。这就是我将布局转换为图像的方式。下面是带有注释的代码:
这是 activity1
上的内容 // here I found the button I wanted to use and made it:
// 1. take us to next activity
// 2. convert my relativelayout to and a bitmap and store it in a variable for transfer.
findViewById<Button>(R.id.DoneButton).setOnClickListener{
val intent = Intent(this, SaveAndNameActivity::class.java)
//here is where I converted my relative layout to a bitmap.
val pictureStackCompleted = findViewById<RelativeLayout>(R.id.pictureStack).drawToBitmap()
//here is where I prepared it for transfer to the next activity (NOTE!: the value here is Parceable? , I chose this bcause it works with bitmaps)
intent.putExtra("pictureStackCompleted" , pictureStackCompleted)
//here is where I told activity2 to start
startActivity(intent)
}```
这是 Activity2 上的内容
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_save_and_name)
//here is where I called the variable containing, my relative layout turned bitmap image
val completedPicture = intent.getParcelableExtra<Bitmap>("pictureStackCompleted")
//here I set my image to be the background image of an ImageView on activity2
val finalImage = findViewById<ImageView>(R.id.finalImage)
finalImage.setImageBitmap(completedPicture)
}
我 100% 确定它与位图转换行有关,因为我测试了那行代码,将它从意图中移到应用程序的另一部分(工作正常)。当我 运行 应用程序的工作部分不再运行时。
原因是您试图使用 Intent
传递 Bitmap
。尽管理论上可行,但 Bitmap
的大小会导致问题,如您的错误所示:
E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 3602192)
如果您想将 Bitmap
传递给另一个 activity,那么我建议:
将
Bitmap
保存到文件将这些添加到您的
manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
那么下面的代码应该可以工作
File bitmapFile = new File(Environment.getExternalStorageDirectory() + "MyBitmap" + ".png"); FileOutputStream fos = new FileOutputStream(bitmapFile); bitmap.compress(Bitmap.CompressFormat.PNG, 80, fos); fos.flush(); fos.close();
使用
将文件路径作为Intent
/Bundle
String
发送到下一个 activityintent.putExtra("filename", "MyBitmap.png");
使用下一个activity中的文件路径重新创建
Bitmap
String fileName = getIntent().getStringExtra("filename"); String filePath = Environment.getExternalStorageDirectory() + fileName; Bitmap bitmap = BitmapFactory.decodeFile(filePath);