Intent.putExtra("photo",bitmap);为什么putExtra会出现错误
时间: 2023-08-02 18:10:32 浏览: 48
出现错误的原因可能是因为您的 `bitmap` 对象太大,超出了 Intent 传递数据的大小限制。Intent 传递数据的大小是有限制的,具体限制取决于您的设备和 Android 版本。如果您传递的数据过大,会导致出现错误并抛出异常。
为了解决这个问题,您可以尝试将 `bitmap` 对象保存在本地文件中,然后将文件路径作为 Intent 的数据传递。或者,您可以使用其他方式来传递 `bitmap`,例如使用 `Bundle` 或使用 `Parcelable` 接口来序列化和反序列化 `bitmap` 对象。
相关问题
安卓获取intent.putExtra("return-data", true)
在 Android 中,如果你要启动一个 Activity 并且希望该 Activity 返回一些数据,可以使用 `startActivityForResult()` 方法。如果你想要该 Activity 返回一个缩略图,可以使用 `Intent.putExtra("return-data", true)`。
当你在调用 `startActivityForResult()` 方法时,你需要传递一个用于接收结果的请求码。当 Activity 将结果返回时,你可以在 `onActivityResult()` 方法中检查该请求码以确定它是哪个 Activity 返回的结果。
下面是一个示例代码片段,演示如何使用 `Intent.putExtra("return-data", true)` 来获取缩略图:
```
// 创建用于获取缩略图的 Intent
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra("return-data", true);
// 启动 Activity 并等待结果
startActivityForResult(intent, REQUEST_CODE_THUMBNAIL);
```
在接收到结果时,可以通过以下方式获取返回的缩略图:
```
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_THUMBNAIL && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap thumbnail = (Bitmap) extras.get("data");
// 处理缩略图
}
}
}
```
需要注意的是,有些设备可能不支持使用 `Intent.putExtra("return-data", true)` 来获取缩略图。在这种情况下,你可以尝试使用 `MediaStore.Images.Media.getBitmap()` 方法来获取缩略图。
Intent intent = new Intent(this, NewActivity.class); intent.putExtra("photo_path", mCurrentPhotoPath); startActivity(intent);这样写之后,在另一个activity怎么接收图片并显示
在另一个Activity中,您可以使用以下代码接收图片并显示:
```
// 获取Intent中传递的图片路径
String photoPath = getIntent().getStringExtra("photo_path");
// 将图片显示在ImageView中
ImageView imageView = findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
imageView.setImageBitmap(bitmap);
```
请注意,这里假设您已经将图片路径作为字符串类型放入了Intent中,并且在另一个Activity中使用了相同的键名("photo_path")来获取该值。同时,也需要在布局文件中添加一个ImageView组件,用于显示图片。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)