首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自Gallery、ImageBitmap和ImageURI的显示图像似乎不起作用

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

Stack Overflow用户
提问于 2020-03-03 22:05:11
回答 1查看 81关注 0票数 0

我想从我的图库加载一张图片,并在我的应用程序中显示它。我试过ImageBitmapImageURI,但我就是不能显示图片。

下面是我的java代码的一部分:

代码语言:javascript
复制
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布局文件的一部分:

代码语言:javascript
复制
<?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错误,权限都被接受。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-03 22:09:47

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

代码语言:javascript
复制
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();
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60508991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档