来自图库的图像未粘贴到Fragment中的ImageView中

时间:2016-05-26 20:53:14

标签: android imageview gallery

我知道这里有很多类似的问题,但我的情况非常愚蠢。或者我。

所以,我有以下简单的布局:

Layout mockup

XML结构:

<RelativeLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/image1"
        android:contentDescription="Description" />

    <TextView/>

    <android.support.design.widget.TabLayout />

    <android.support.v4.view.ViewPager />

    <Button />

</RelativeLayout>

打开按钮点击库,用户选择一些图像并将其放入ImageView

我使用以下代码创建了测试活动,并且它非常完美:

public class TestActivity extends AppCompatActivity {
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        imageView = (ImageView) findViewById(R.id.image1);
        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");

                startActivityForResult(intent, 10);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10 && resultCode == RESULT_OK) {
            Uri uri = data.getData();
            imageView.setImageURI(uri);
        }

    }
}

标记:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:id="@+id/image1"
        android:layout_above="@+id/button1"
        android:contentDescription="image" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pick Image"
        android:id="@+id/button1"
        android:layout_gravity="center"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

在原始项目中,此代码不起作用。快速调用Gallery,所有方法也被调用,但图像不会粘贴到ImageView中:

public class MainFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

...

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent chooserIntent = new Intent(Intent.ACTION_PICK);
                chooserIntent.setType("image/*");

                startActivityForResult(chooserIntent, REQUEST_CODE_PICK_IMAGE);
            }
        });

}

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();
            imageView.setImageURI(uri);

            Toast.makeText(getContext(), "Image is picked successfully", Toast.LENGTH_LONG).show();
        }   
}

到目前为止,我尝试使用以下代码段READ_EXTERNAL_STORAGE(以防万一):

Snippet 1

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();

            try {
                InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                imageView.setImageBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }       
}   

Snippet 2

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), uri);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }       
}

摘录3

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();

            try {
                InputStream inputStream = getActivity().getContentResolver().openInputStream(uri);
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                Drawable drawable = new BitmapDrawable(getResources(), bitmap);
                imageView.setImageDrawable(drawable);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }       
}

摘录4

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();

            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContext().getContentResolver().query(uri, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }       
}

还没有结果。我不知道可能是什么问题。我将非常感谢任何帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

在发布问题30分钟之后,我突然找到了答案

由于我的ImageView处于片段状态,因此在图像拾取后立即启动并重新创建库时,它将被销毁。在重新创建过程中,使用初始值绘制ImageView,并且丢失所选图像的Uri

解决方案是将Uri保存在某处并将其应用于使用条件语句创建片段。或者以某种方式阻止片段娱乐

我希望它可能对某人有帮助