我在Activity1中有一个名为bmp
的Bitmap变量,我想将位图发送到Activity2
以下是我用来传递意图的代码。
Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",bmp);
startActivity(in1);
在Activity2中,我尝试使用以下代码访问位图
Bundle ex = getIntent().getExtras();
Bitmap bmp2 = ex.getParceable("image");
ImageView result = (ImageView)findViewById(R.Id.imageView1);
result.setImageBitmap(bmp);
应用程序无异常地运行,但它没有给出预期的结果
答案 0 :(得分:172)
在将其添加到intent之前将其转换为Byte数组,将其发送出去并解码。
//Convert to byte array
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",byteArray);
然后在活动2中:
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
修改强>
我想我应该用最佳实践来更新它:
在第一个活动中,您应该将位图保存到磁盘,然后在下一个活动中加载它。确保在第一个活动中回收您的位图以填充它以进行垃圾收集:
活动1:
try {
//Write file
String filename = "bitmap.png";
FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
//Cleanup
stream.close();
bmp.recycle();
//Pop intent
Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image", filename);
startActivity(in1);
} catch (Exception e) {
e.printStackTrace();
}
在活动2中,加载位图:
Bitmap bmp = null;
String filename = getIntent().getStringExtra("image");
try {
FileInputStream is = this.openFileInput(filename);
bmp = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
干杯!
答案 1 :(得分:6)
有时,位图可能对于编码和解码来说太大或者在意图中作为字节数组传递。这可能会导致OOM或糟糕的UI体验。
我建议考虑将位图放入新活动的静态变量(使用它的那个),当你不再需要它时,它会小心地为空(意味着在onDestroy中,但只有当“isChangingConfigurations”返回false时)。
答案 2 :(得分:1)
我们只能传递Bitmap的Uri而不是传递Bitmap对象。如果Bitmap对象为Big,则会导致内存问题。
FirstActivity。
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),Uri.parse(uri));
从SecondActivity我们得到位图。
import (
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/aws/session"
"fmt"
)
func NewIAM() *SphinxIAM {
// awsConfig := aws.NewConfig()
sess, err := session.NewSession()
if err != nil {
fmt.Println("Failed to create session,", err)
return nil
}
session := &SphinxIAM{iam: iam.New(sess)}
return session
}
答案 3 :(得分:0)
我们也可以通过意图传递数据来解决这个问题。只需将图像存储在内存中,然后在下一个Activity中加载来自该位置的图像,这也可以避免应用程序因传递大位图数据而崩溃。 注意:您甚至不需要将位置路径传递给intent,记住路径并使用它。
答案 4 :(得分:0)
我还想为那些希望这样做的人发布最佳实践答案(但可能会提出错误的问题)。
我没有传递位图(我认为你是从网络上下载的,否则,你已经有了一个文件引用),我建议使用Universal Image Loader之类的图像加载器将图像下载到一个ImageView。您可以将其配置为然后将映像缓存到磁盘:
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.build();
ImageLoader.getInstance().init(config);
现在,只需在您的意图中传递图像URL,然后使用UIL加载图像。例如,在新创建的活动中,图像将立即加载,因为它是从缓存加载的 - 即使您的图像URL自下载以来已过期。
答案 5 :(得分:0)
科特琳代码,用于通过意图将位图发送到另一个活动:
1-在第一个活动中:
val i = Intent(this@Act1, Act2::class.java)
var bStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 50, bStream)
val byteArray = bStream.toByteArray()
i.putExtra("image", byteArray )
startActivity(i)
2-在活动二中(读取位图图像):
var bitmap : Bitmap? =null
if (intent.hasExtra("image")){
//convert to bitmap
val byteArray = intent.getByteArrayExtra("image")
bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
}
3-如果需要将其设置为视图的背景,例如ConstraintLayout或...:
if (bitmap != null) {
//Convert bitmap to BitmapDrawable
var bitmapDrawable = BitmapDrawable( resources , bitmap)
root_constraintLayout.backgroundDrawable = bitmapDrawable
}
答案 6 :(得分:-6)
Bundle b = new Bundle();
b.putSerializable("Image", Bitmap);
mIntent.putExtras(b);
startActivity(mIntent);