Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示来自Gallery的图像,ImageBitmap和ImageURI似乎不起作用_Java_Android_Android Layout_Imageview - Fatal编程技术网

Java 显示来自Gallery的图像,ImageBitmap和ImageURI似乎不起作用

Java 显示来自Gallery的图像,ImageBitmap和ImageURI似乎不起作用,java,android,android-layout,imageview,Java,Android,Android Layout,Imageview,我想从我的图库中加载一张图片并在我的应用程序中显示它。我尝试了ImageBitmap和ImageURI,但我就是不显示图片 以下是我的java代码的一部分: private ListView LIST_Photos; private TextView TV_SessionName, TV_SessionInfo; private ImageView IV_Picture; private ArrayList<HashMap<String, String>> photos;

我想从我的图库中加载一张图片并在我的应用程序中显示它。我尝试了ImageBitmap和ImageURI,但我就是不显示图片

以下是我的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错误,并且所有权限都被接受。

使用位图您可以使用位图直接设置图像

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();
        }
    }
}

扔掉所有那些数据。将图像的Uri传递到您最喜欢的图像加载库,例如Glide、Picasso。代码太多。尝试imageView.setImageURIdata.getData;它起作用了。谢谢你,尽管有些照片相机文件夹里的照片没有显示出来。知道为什么吗?