Android Studio:显示我的 Android 画廊中的图像

问题描述 投票:0回答:1

我是 Java 和 Android Studio 编程新手。我正在尝试创建一个简短的应用程序,感谢这里其他程序员的帖子:一个带有“从图库浏览”按钮的应用程序 - >当我点击它时,我的android图库被打开,然后我选择一个图像,我想要它显示在我创建的 imageView 上。

按下按钮并选择图像后,我成功访问了我的 Android 图库,但 imageView 上没有显示任何内容。

这是 main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".BlankActivityWithFragment" tools:ignore="MergeRootFrame">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Browse from Gallery"
        android:id="@+id/button"
        android:layout_gravity="left|top"
        android:onClick="onButtonClicked"
        android:enabled="true" />

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#ffaaaa"
        android:id="@+id/imageViewGallery"
        android:layout_gravity="center_horizontal|top" />

</FrameLayout>

如你所见,Browse From Gallery 的按钮会调用 onButtonClicked 函数。

这是我的java代码:

public class ImagesProj extends Activity {

    // define the variable that will show the gallery images
    ImageView imageViewGallery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.images_proj);

        // connect the variable to the images_proj.xml
        imageViewGallery = (ImageView) findViewById(R.id.imageViewGallery);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

    }

    // This function is called after clicking the Browse From Gallery button
    public boolean onButtonClicked(View view) {

        // access the images gallery
        Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 2);

        return true;
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 2 && 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();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            // String picturePath contains the path of selected Image

            // Show the Selected Image on ImageView
            ImageView imageView = (ImageView) findViewById(R.id.imageViewGallery);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }
    }
}

请帮我显示所选的图像,谢谢!

我试图查看的帖子: 从图库中选择照片并在图像视图中显示

android onclick imageview gallery
1个回答
0
投票

可能图像很大,仅仅在路径上调用decodeFile不会将位图调整到imageView。

使用库(像大多数人一样:P)或研究将其调整为 imageView 尺寸的方法。顺便说一句,库将为您节省大量精力,但您自己做就会变得更好。

© www.soinside.com 2019 - 2024. All rights reserved.