[英]Cannot display the selected image from mobile storage (Android Studio)
My code is working fine for selecting images from the phone, but I can't see a preview before uploading it, why? 我的代码可以很好地从手机中选择图像,但是在上传之前看不到预览,为什么? what's wrong? 怎么了?
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final int PIC_ID = 1;
Button uButton, dButton;
EditText firstText, secondText;
ImageView firstImage, secondImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uButton = (Button)findViewById(R.id.uButton);
dButton = (Button)findViewById(R.id.dButton);
firstText = (EditText)findViewById(R.id.firstText);
secondText = (EditText)findViewById(R.id.secondText);
firstImage = (ImageView)findViewById(R.id.firtsImage);
secondImage = (ImageView)findViewById(R.id.secondImage);
firstImage.setOnClickListener(this);
uButton.setOnClickListener(this);
dButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.firtsImage:
Intent imageIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(imageIntent, PIC_ID);
break;
case R.id.dButton:
break;
case R.id.uButton:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PIC_ID && resultCode == RESULT_OK && data != null){
Uri selected = data.getData();
firstImage.setImageURI(selected);
}
}
and my activity_main.xml file is 我的activity_main.xml文件是
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/firtsImage"
android:layout_gravity="center_horizontal"
android:layout_width="150dp"
android:layout_height="150dp" />
<EditText
android:id="@+id/firstText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/uButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Upload"/>
<ImageView
android:id="@+id/secondImage"
android:layout_gravity="center_horizontal"
android:layout_width="150dp"
android:layout_height="150dp" />
<EditText
android:id="@+id/secondText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/dButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="download"/>
I've googled it until I got a headache found nothing. 我用谷歌搜索,直到头疼没发现。
You can do following: 您可以执行以下操作:
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
firstImage.setImageBitmap(bitmap);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.