我正在尝试使用Android将我的galary中的图片加载到表单中

时间:2015-11-03 16:16:34

标签: android button parse-platform android-imageview

我有一个图像视图和一个按钮,所以当我点击按钮时,我会全部选择图像,但它没有加载到视图上。我不确定我做错了什么....请注意附加代码,我在使用Android时相当新。 我希望图像视图具有我从galary中选取的图像。我可能会错过一小段或非常大的代码,我不确定。请帮忙

if (v.getId() == R.id.btnPickImage) {
                onImageGalleryClicked(v);
            }
        }

        public void onImageGalleryClicked(View v) {
            Intent pick = new Intent(Intent.ACTION_PICK);

            File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            String picture = pictureDirectory.getPath();

            Uri data = Uri.parse(picture);

            pick.setDataAndType(data, "image/*");
            //Start new activity with the LOAD_IMAGE_RESULT to handle back the result when the image is picked from the Image Gallery
            startActivityForResult(pick, LOAD_IMAGE_RESULTS);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == RESULT_OK) {
                //Everything Okay
                if (requestCode == LOAD_IMAGE_RESULTS) {
                    Uri pickedImage = data.getData();
                    InputStream inputStream;
                    try {
                        inputStream = getContentResolver().openInputStream(pickedImage);
                        Bitmap selectedImages = BitmapFactory.decodeStream(inputStream);
                        image.setImageBitmap(selectedImages);

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Unable to load image",
                                Toast.LENGTH_LONG).show();
                    }
                }
            }
        }

这是我机器上的xml。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="10dip" >


        <ImageView
            android:id="@+id/imgView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:contentDescription="@string/content"
            android:layout_alignTop="@+id/btnPickImage"
            android:layout_toLeftOf="@+id/btnPickImage"
            android:layout_toStartOf="@+id/btnPickImage"
            android:layout_alignBottom="@+id/btnPickImage" />

        <Button
            android:id="@+id/btnPickImage"
            android:onClick="pickImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/txtLoadImage"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/textView3"
            android:layout_alignEnd="@+id/textView3" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

在你的onActivityResult()中,第一行应该是

if (resultCode == RESULT_OK) {

答案 1 :(得分:0)

我没有看到image被宣布。

你需要这样的东西:

ImageView image = (ImageView) findViewById(R.id.imgView);

在致电

之前
image.setImageBitmap(selectedImages);