Android中使用Intent调用图库无法显示图片问题求助
Let's break down the potential problems in your code and configurations, then fix them step by step:
1. Missing Runtime Permission Request
On Android 6.0 (API level 23) and above, declaring permissions in AndroidManifest.xml is only half the job—you need to dynamically request storage permissions at runtime. The system won't grant READ_EXTERNAL_STORAGE automatically even if it's listed in the manifest, unless you explicitly ask the user for access.
Fix:
Add permission checking logic before launching the gallery intent:
private static final int PICK_IMAGE = 100; private static final int PERMISSION_REQUEST_CODE = 101; // Inside your onClick handler for the "CHOOSE PHOTO" button case R.id.empchoosephoto: if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // Request permission if not yet granted ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); } else { // Permission already granted, launch gallery directly Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); startActivityForResult(gallery, PICK_IMAGE); } break;
Then handle the permission request result:
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == PERMISSION_REQUEST_CODE) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission approved, launch gallery now Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); startActivityForResult(gallery, PICK_IMAGE); } else { // Permission denied, inform the user Toast.makeText(this, "Storage permission is required to select images", Toast.LENGTH_SHORT).show(); } } }
2. Flawed onActivityResult Logic
Your current onActivityResult code has two critical issues:
- You're using a vague check
requestCode!=0instead of verifying it matchesPICK_IMAGE - You're not confirming
resultCode == RESULT_OK(this ensures the user didn't cancel the selection)
Fix:
Update your onActivityResult method to validate the request properly:
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); // Verify it's our image pick request and the result is valid if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) { if (data!= null) { empimageUri = data.getData(); empimage.setImageURI(empimageUri); } } }
3. Uninitialized ImageView
In your code snippet, there's no sign that you've linked the empimage ImageView to your layout with findViewById. If this view isn't initialized, setting the image URI will have no visible effect.
Fix:
Add the initialization in your onCreate method:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_employee); choosephoto = (LinearLayout) findViewById(R.id.empchoosephoto); // Add this line (replace R.id.your_image_view_id with your actual ImageView ID from the layout) empimage = (ImageView) findViewById(R.id.your_image_view_id); choosephoto.setOnClickListener(this); }
4. Android 10+ Scoped Storage Compatibility (Optional)
If your app targets Android 10 (API level 29) or higher, scoped storage might restrict media file access. While ACTION_PICK should work with media store URIs, you can add a temporary compatibility flag to rule this out:
Add this attribute to your <application> tag in AndroidManifest.xml:
android:requestLegacyExternalStorage="true"
内容的提问来源于stack exchange,提问作者no_profile

