Android应用问题:相册选图后无法在另一Activity显示
问题排查与修复方案
核心问题分析
- 请求码使用错误:
startActivityForResult(intent, RESULT_OK)中,RESULT_OK是系统定义的结果码(值为-1),应使用自定义请求码(如1)标识相册选择请求,避免与结果码混淆。 - 废弃API导致路径失效:
MediaStore.Images.Media.DATA在Android 10(API 29)及以上已被废弃,直接通过该字段获取的文件路径可能为空或无权限访问,导致BitmapFactory.decodeFile(filePath)返回null。 - Bitmap跨页传递风险:直接通过Intent传递Bitmap,若图片尺寸较大,会超过Binder传输限制,导致Bitmap无法正常传递到目标Activity。
修复方案
1. 替换废弃的路径获取逻辑
不再通过文件路径加载Bitmap,直接通过Uri使用ContentResolver打开输入流加载图片,适配高版本Android权限规范。
2. 改用Uri跨Activity传递数据
传递图片的Uri字符串而非Bitmap,既规避大小限制,又符合Android数据传递规范。
3. 规范请求码使用
自定义请求码常量,明确标识本次相册选择请求。
修改后的代码
MainActivity.java
public class MainActivity extends AppCompatActivity { private static final int PICK_IMAGE_REQUEST = 1; // 自定义请求码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void buttonGalleryOpen(View view) { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, PICK_IMAGE_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); // 同时校验请求码和结果码 if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) { Uri selectedImageUri = data.getData(); if (selectedImageUri != null) { Intent intent = new Intent(MainActivity.this, gallery_view.class); intent.putExtra("image_uri", selectedImageUri.toString()); // 传递Uri字符串 startActivity(intent); } } } }
gallery_view.java
public class gallery_view extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gallery_view); ImageView imageView = findViewById(R.id.ImageShow); String imageUriStr = getIntent().getStringExtra("image_uri"); if (imageUriStr != null) { Uri imageUri = Uri.parse(imageUriStr); // 通过ContentResolver加载图片 try { InputStream inputStream = getContentResolver().openInputStream(imageUri); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); imageView.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); } } } }
额外修复:gallery_view.xml的tools:context错误
原布局中tools:context=".MainActivity"指向错误,修正为当前Activity:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".gallery_view"> <!-- 修正此处 --> <RelativeLayout android:id="@+id/relative_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="camera sample" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </RelativeLayout> <RelativeLayout android:layout_below="@id/relative_1" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/ImageShow" android:layout_width="match_parent" android:layout_height="500dp" /> </RelativeLayout> </RelativeLayout>
内容的提问来源于stack exchange,提问作者Jethro
相关产品推荐
相关产品推荐

