1

私はフラグメント (API 4.1) を使用しており、ユーザーがボタンを押してギャラリーにアクセスし、画像を選択して、その画像にボタンが表示された元のフラグメントの画像ビューを表示させたいと考えています。私は次のコードを使用しています:

public class FillBox1Frag extends Fragment {
Button addPics, placeBox;
ImageView imgView;
Bitmap b;
Uri photoUri;
LinearLayout fillBoxLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    if (container == null) {
        return null;
    }

    fillBoxLayout = (LinearLayout) inflater.inflate(R.layout.fillbox1_frag,
            container, false);
    newBin = new Bin();
    addPics = (Button) fillBoxLayout.findViewById(R.id.bPics);
    imgView = (ImageView) fillBoxLayout.findViewById(R.id.imageView1);
    addPics.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent picChooser = new Intent(Intent.ACTION_GET_CONTENT,
                    MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            picChooser.setType("image/*");
            startActivityForResult(picChooser, 12345);
        }
    });
    return fillBoxLayout;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case 12345:
        if (resultCode == 12345) {
            photoUri = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = FillBox1Frag.this.getActivity()
                    .getContentResolver()
                    .query(photoUri, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            b = BitmapFactory.decodeFile(filePath);
            imgView.setImageBitmap(b);
        }
    }
}

ギャラリーにアクセスしましたが、選択すると元のフラグメントに戻るだけで、画像が表示されません。ここに私のXMLがあります:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fillbox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="fillbox" >

<Button
    android:id="@+id/bPics"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="89dp"
    android:background="@drawable/buttonpics" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

同様の質問に対処する多くの回答を見つけましたが、フラグメントを使用してこのプロセスを作成する方法について私を助けることができたものは何もありません. 選択した画像を ImageView に表示する方法についてのアイデアはありますか? 助けてくれてありがとう!

4

0 に答える 0