【问题标题】:Android Studio: display images from my android galleryAndroid Studio:显示来自我的 android 画廊的图像
【发布时间】:2015-05-25 14:46:06
【问题描述】:

我是 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));

        }
    }
}

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

我试图查看的帖子: Picking a photo from gallery and show in a image view

【问题讨论】:

  • 您是否遇到任何错误/崩溃,还是只有图像没有出现?
  • 我没有收到任何错误/崩溃。图片只是没有出现
  • 您的代码看起来不错,您是否验证过 onActivityResult 方法已按预期调用?
  • 是的,我调试的时候看起来还不错
  • 请在此处评论某个角图像的图像路径

标签: android onclick imageview gallery


【解决方案1】:

可能是图片很大,直接在路径上调用decodeFile不会把位图调整到imageView。

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

【讨论】:

  • 有趣!我会试试这个,让你知道..谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 2015-08-30
相关资源
最近更新 更多