2 回答

TA貢獻1798條經驗 獲得超3個贊
首先,您必須獲得用戶的許可并打開圖庫意圖。當您找到圖像 url 時,您需要使用像 glide 這樣的圖像加載器庫來顯示它。單擊按鈕時,您需要做的最后一件事是檢查圖像 url 是否為空。如果您需要進一步的幫助,請告訴我。

TA貢獻1829條經驗 獲得超9個贊
在 onCreate 方法中添加
newPostImgbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getImageFromAlbum();
}
});
在你的活動課上
private void getImageFromAlbum(){
try{
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}catch(Exception exp){
Log.i("Error",exp.toString());
}
}
將此方法添加到您的活動類
Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
newPostImage .setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}else {
//image not selected so do what you want when image is not selected
}
添加回答
舉報