gpt4 book ai didi

java - 显示图库中的图像,ImageBitmap 和 ImageURI 似乎不起作用

转载 作者:行者123 更新时间:2023-12-02 08:55:38 25 4
gpt4 key购买 nike

我想从我的画廊加载图片并将其显示在我的应用程序中。我尝试了 ImageBitmapImageURI 但我只是不显示图片。

这是我的java代码的一部分:

private ListView LIST_Photos;
private TextView TV_SessionName, TV_SessionInfo;
private ImageView IV_Picture;
private ArrayList<HashMap<String, String>> photos;
private Session session;
private static int RESULT_LOAD_IMAGE = 1;
private ImageView imageView;

public void addPhotograph(View view){
/*Intent intent = new Intent(this, NewPhotographActivity.class);
startActivity(intent);//*/
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 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();
String picturePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
cursor.close();
File imgFile = new File(picturePath);
imageView = (ImageView) findViewById(R.id.IV);

imageView.setImageBitmap(BitmapFactory.decodeFile(imgFile.getAbsolutePath()));

imageView.setImageURI(selectedImage);

imageView.setImageURI(Uri.fromFile(imgFile));
}
}

这是 xml 布局文件的一部分:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:id="@+id/IV"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:srcCompat="@tools:sample/avatars[0]" />

</LinearLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/BT_addPhotograph"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:clickable="true"
android:focusable="true"
android:onClick="addPhotograph"
app:borderWidth="0dp"
app:elevation="0dp"
app:hoveredFocusedTranslationZ="0dp"
app:maxImageSize="42dp"
app:pressedTranslationZ="0dp"
app:srcCompat="@drawable/add" />

</FrameLayout>

这是我在网上找到的所有内容,我什至尝试复制功能应用程序,但它也不起作用。我没有收到任何运行时或 Logcat 错误,并且权限均已接受。

最佳答案

使用bitmap可以直接使用Bitmap设置Image

public void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

Uri uri = data.getData();

try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));

ImageView imageView = findViewById(R.id.IV);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}

关于java - 显示图库中的图像,ImageBitmap 和 ImageURI 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60508991/

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