Android 中用Intent如何传递Bitmap路径
时间: 2024-01-24 20:05:36 浏览: 180
要在 Android 中使用 Intent 传递 Bitmap 路径,您可以将 Bitmap 保存到本地文件中,然后将文件路径添加到 Intent 的 extras 中。以下是一个示例代码:
```java
// 将Bitmap保存到本地文件
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path, "bitmap.png");
FileOutputStream stream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.flush();
stream.close();
// 创建Intent对象
Intent intent = new Intent(this, YourActivity.class);
// 添加Bitmap文件路径到Intent的extras中
intent.putExtra("bitmapPath", file.getAbsolutePath());
// 启动目标Activity
startActivity(intent);
```
然后在接收 Intent 的目标 Activity 中,您可以从 extras 中获取文件路径,并将其转换回 Bitmap。以下是一个示例代码:
```java
// 获取传递过来的Bitmap文件路径
String path = getIntent().getStringExtra("bitmapPath");
// 把文件路径转换成Bitmap
Bitmap bitmap = BitmapFactory.decodeFile(path);
```
请注意,为了避免出现权限问题,您需要在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
```
阅读全文
相关推荐


















