简体   繁体   English

无法显示移动存储中的所选图像(Android Studio)

[英]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.

相关问题 从 HTTPS 下载图像并上传到 Android Studio 中的 firebase 存储 - Download image from HTTPS and upload to firebase storage in Android Studio 从 Firebase 存储下载图像到 imageview 上 android 工作室 Z93F725A07423FE2189F448B336 - Downloading image from Firebase storage into imageview on android studio java 无法在 android studio 中以编程方式从外部存储中删除图像 - Not able to delete image from external storage programmatically in android studio Android:从 SharedPreferences 中的内部存储保存图像并显示它 - Android: Save image from Internal storage in SharedPreferences and display it 如何从图像库中获取所选图像的名称 android studio - how to get the name of a selected image from gallery of images android studio android studio 无法在图像视图中显示图像 - android studio cannot show the image in the image view Android-在不压缩的情况下将位图图像保存在移动存储中 - Android - Save Bitmap Image on Mobile Storage without Compressing 从相机拍摄的图像无法在图像视图android 5.0中显示,但在android 4.4中工作 - Image taken from camera cannot display in image view android 5.0, but working in android 4.4 如何在 android 工作室中使用声音云从图库中选择裁剪图像的 uri - how to get the uri of a cropped image selected from gallery using sound cloud in android studio 如何在 Android Studio 中的图像上显示矩形 - How to display a rectangle over an Image in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM