gpt4 book ai didi

java - 将图像加载到 imageView 中

转载 作者:行者123 更新时间:2023-12-01 13:47:38 25 4
gpt4 key购买 nike

我是一名新的 Android 开发人员,我的第一个 Intent 任务是从图库中选择一张图像并将其显示到 ImageView 中,所以我所做的如下:

xml:

 <Button
android:id="@+id/Intent_btn"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/button1"
android:text="Intent button"
android:onClick="openGallery" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignRight="@+id/Intent_btn"
android:layout_centerVertical="true" />

代码:

ImageView imageview1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview1=(ImageView)findViewById(R.id.imageView1);
}

public void openGallery(View v)
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
imageview1.setImageBitmap(yourSelectedImage);
}
}

我设法打开图库并选择一张图像,但它从未加载,这会是什么问题?

最佳答案

向 list 文件添加权限:

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

关于java - 将图像加载到 imageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20240598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com