簡體   English   中英

顯示圖庫、ImageBitmap 和 ImageURI 中的圖像似乎不起作用

[英]Display Image from Gallery, ImageBitmap and ImageURI don't seem to work

我想從我的畫廊加載圖片並將其顯示在我的應用程序中。 我嘗試了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 錯誤,並且所有權限都被接受。

使用位圖,您可以使用位圖直接設置圖像

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM