Отображение изображения из галереи, ImageBitmap и ImageURI не работают - PullRequest
0 голосов
/ 03 марта 2020

Я хочу загрузить изображение из моей галереи и отобразить его в моем приложении. Я пробовал 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>

Это все, что я нашел в Интернете, и я даже пытался скопировать функциональное приложение, но оно тоже не работает. Я не получаю никаких ошибок Runtime или Logcat, и все разрешения приняты.

1 Ответ

1 голос
/ 03 марта 2020

Используйте растровое изображение, вы можете установить изображение непосредственно с помощью растрового изображения

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