我正在学习在 Android Studio 中编程,我正在制作一个带有 Firebase 观看教程的应用程序,在这一部分中 https://www.youtube.com/watch?v=tG6Vqi7Ur9A(是仅 4 分钟的持续时间的视频(我按照教程的说明进行操作,但是当我在模拟器中运行该应用程序并尝试从我的图库中选择一个图像时,单击图像按钮,图像显示为半透明或灰色, 我尝试单击它们,但从未被选中,它被禁用选择。
我澄清说我的模拟器中没有图像,所以我将它们从桌面拖到 android 模拟器,所以现在如果它出现,我不知道这是否会影响。我将其添加到清单中,
<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" />
我的爪哇类
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);
}
}
作为一种选择,您可以尝试另一种方式
Val intent = Intent(Intent_ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, 111)
这是访问图库的简单方法
其余代码onActivityResult
您可以按照图片中的样式进行操作(但在 Kotlin 和 java 之间略有变化(