java - 我无法从我的图库中选择图像进行上传(android studio)

标签 java android gallery imagebutton

我正在学习在 Android Studio 中编程,我正在使用 Firebase 制作一个应用程序,观看教程,在这部分 https://www.youtube.com/watch?v=tG6Vqi7Ur9A (是一个持续时间只有 4 分钟的视频)我按照教程的说明进行操作,但是当我在模拟器中运行应用程序并尝试从图库中选择图像时,单击 ImageButton,图像显示为半透明或灰色,我尝试单击但从未被选中,无法选择。

我澄清说,我的模拟器中没有图像,因此我将它们从桌面拖到了 Android 模拟器中,所以现在如果它出现,我不知道这是否会产生影响。我将其添加到 MANIFEST 中,

<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

但它不起作用。

我的添加图像布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_post"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.social.socialpart.socialpart.PostActivity">


<ImageButton
    android:layout_marginTop="15dp"
    android:layout_width="250dp"
    android:layout_height="150dp"
    android:id="@+id/imageButton"
    android:background="@color/transparent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/post"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true" />

<EditText
    android:background="@color/common_google_signin_btn_text_dark_pressed"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="title"
    android:ems="10"
    android:layout_below="@+id/imageButton"
    android:layout_centerHorizontal="true"
    android:id="@+id/CreatePost_title" />

<EditText
    android:background="@color/common_google_signin_btn_text_dark_pressed"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Description"
    android:ems="10"
    android:layout_below="@+id/CreatePost_title"
    android:layout_centerHorizontal="true"
    android:id="@+id/CreatePost_description" />

<Button
    android:text="Submit post"
    android:background="@color/colorPrimary"
    android:textColor="@color/cardview_light_background"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Publicar_button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

我的Java类

    public class PostActivity extends AppCompatActivity {

private ImageButton mSlectImage;
private static final int GALLERY_REQUEST=1;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);
    mSlectImage=(ImageButton) findViewById(R.id.imageButton);
    mSlectImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent=new Intent(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("Image/*");
            startActivityForResult(galleryIntent,GALLERY_REQUEST);
        }
 });
   @Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
    super.onActivityResult(requestCode,resultCode,data);

    if (requestCode==GALLERY_REQUEST && resultCode==RESULT_OK){
        Uri imageUri=data.getData();
        mSlectImage.setImageURI(imageUri);
    }

}

}

对不起,希望你理解我,我的英语很正常。

最佳答案

public class PostActivity extends AppCompatActivity {

    private ImageButton mSlectImage;
    private static final int GALLERY_REQUEST=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);
        mSlectImage=(ImageButton) findViewById(R.id.imageButton);
        mSlectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseImage();
            }
        });
        private void chooseImage()
        {
            Intent galleryIntent=new Intent(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/*");
            startActivityForResult(galleryIntent,GALLERY_REQUEST);
        } 
        @Override
        public void onActivityResult(int requestCode,int resultCode,Intent data){
            super.onActivityResult(requestCode,resultCode,data);

            if (requestCode==GALLERY_REQUEST && resultCode==RESULT_OK){
                Uri imageUri=data.getData();
                mSlectImage.setImageURI(imageUri);
            }
        }

关于java - 我无法从我的图库中选择图像进行上传(android studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42054501/

相关文章:

php - woocommerce 中的产品图库附件 url

java - JNI 上的 Java native 崩溃

java - jackson 轻松服务的主要方法

java - 如何将 BufferedImage 对象保存到文件?

Android P - 从 Assets 复制数据库后出现 'SQLite: No Such Table Error'

java - 如何在服务器端使用 Google 应用内购买

android - 如何使用fcm在android中实现多个通知onclick?

java - 如何长时间从图库中获取图片Uri?

java - 如何销毁一个线程,暂停/挂起一个线程,恢复/运行一个线程?

android - 延迟加载(异步)图像时如何防止图库对齐页面