为什么不能在从移动模拟器存储中挑选图片的 ImageView 中显示图片?
Why can't show pic in ImageView which pick pic from mobile simulator's storage?
public class MainActivity extends AppCompatActivity {
private static final int PICK_IMAGE_REQUEST = 1;
private Button mButtonChooseImage;
private Button mButtonUpload;
private TextView mTextViewShowUploads;
private EditText mEditTextFileName;
private ImageView mImageView;
private ProgressBar mProgressBar;
private Uri mImageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonChooseImage = findViewById(R.id.button_choose_image);
mButtonUpload = findViewById(R.id.button_upload);
mTextViewShowUploads = findViewById(R.id.text_view_show_upload);
mEditTextFileName = findViewById(R.id.edit_text_file_name);
mImageView = findViewById(R.id.image_view);
mProgressBar = findViewById(R.id.progress_bar);
mButtonChooseImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFileChooser();
}
});
mButtonUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
mTextViewShowUploads.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void openFileChooser(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
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 && requestCode == RESULT_OK
&& data != null && data.getData() != null ){
mImageUri = data.getData();
mImageView.setImageURI(mImageUri);
}
}
}
Problem snapshot
↑问题快照请点这里
问题结果空图:
↑黄线为未显示图片的结果
When I chose photo from mobile storage, I couldn't show photo in ImageView(mImageView) like pic link above.
Please tell me why?
我从来没有直接将选定的图像放在 ImageView 上,但我已经使用 Glide 库完成了同样的工作。我推荐你使用它。
如何使用滑行将选定的图像放入图像视图中:
Glide.with(context)
.load(path)
.into((ImageView) findViewById(imageViewId));
其中path是选择的图片路径。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == requst && resultCode == RESULT_OK && data != null) {
Uri uri = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
mImageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class MainActivity extends AppCompatActivity {
private static final int PICK_IMAGE_REQUEST = 1;
private Button mButtonChooseImage;
private Button mButtonUpload;
private TextView mTextViewShowUploads;
private EditText mEditTextFileName;
private ImageView mImageView;
private ProgressBar mProgressBar;
private Uri mImageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonChooseImage = findViewById(R.id.button_choose_image);
mButtonUpload = findViewById(R.id.button_upload);
mTextViewShowUploads = findViewById(R.id.text_view_show_upload);
mEditTextFileName = findViewById(R.id.edit_text_file_name);
mImageView = findViewById(R.id.image_view);
mProgressBar = findViewById(R.id.progress_bar);
mButtonChooseImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFileChooser();
}
});
mButtonUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
mTextViewShowUploads.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void openFileChooser(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
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 && requestCode == RESULT_OK
&& data != null && data.getData() != null ){
mImageUri = data.getData();
mImageView.setImageURI(mImageUri);
}
}
}
Problem snapshot
↑问题快照请点这里
问题结果空图:
↑黄线为未显示图片的结果
When I chose photo from mobile storage, I couldn't show photo in ImageView(mImageView) like pic link above. Please tell me why?
我从来没有直接将选定的图像放在 ImageView 上,但我已经使用 Glide 库完成了同样的工作。我推荐你使用它。
如何使用滑行将选定的图像放入图像视图中:
Glide.with(context)
.load(path)
.into((ImageView) findViewById(imageViewId));
其中path是选择的图片路径。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == requst && resultCode == RESULT_OK && data != null) {
Uri uri = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
mImageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}