问题
I have a very weird issue, I want the user to select an image and display it on a ImageView, the code works perfectly on my Xiaomi mi52 (V4.1.1) and my Xiaomi MiPad (V4.4.4) but it's totally failed on my Nexus 5 (V5.0).
public class CreateContactActivity extends Activity {
private ImageView imgProfile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_contact);
imgProfile = (ImageView)findViewById(R.id.imgProfileIV);
}
public void ImageProfile(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 100);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 100:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
imgProfile.setImageURI(selectedImage);
}
}
}
}
Layout activity_create_contact.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<ImageView
android:id="@+id/imgProfileIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_person"
android:onClick="ImageProfile" />
</LinearLayout>
On LogCat I get these errors:
12-10 15:33:18.348 D/skia﹕ --- SkImageDecoder::Factory returned null
12-10 15:33:18.350 I/System.out﹕ resolveUri failed on bad bitmap uri: content://media/external/images/media/72
The weird this is it's just on my Nexus 5, not on the Xiaomi devices. I'm looking and looking but I don't understand where is the problem.
Thanks for your help.
回答1:
The difference in result may come from the "picker" you chose on different platform.
You should try to do control experiment by installing same file explorer app as picker so that we can make sure the problem comes from difference in Android version
来源:https://stackoverflow.com/questions/27403703/select-image-from-gallery-and-display-it-on-imageview-issue-on-nexus-5