单击列表视图上的图像uri时显示图像

时间:2014-01-02 23:23:01

标签: android image listview android-listview

我有一个包含listview和imagebutton的片段。图像按钮用于选择图像并将图像从手机图库附加到列表视图。

现在我要做的是,如果我点击列表视图中的图像uri,将弹出完整视图或缩略图(以较为优先者为准)。

我做的是,我设置了一个带有imageview的自定义布局的对话框。每当我点击图像uri时,都会出现对话框。但当然,它还没有任何图像。

那么如何在其上显示图像uri的完整视图或缩略图?

片段:

public class Fragment1 extends Fragment implements OnClickListener {

    ListView lv;
    ArrayList<Uri> array_list = new ArrayList<Uri>();
    ArrayAdapter<Uri> array_adapter;

    final int RQS_LOADIMAGE = 0;

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

        View v = inflater.inflate(R.layout.fragment, container, false); 

        ImageButton b = (ImageButton) v.findViewById(R.id.button_ai);
        b.setOnClickListener(this);

        array_adapter = new ArrayAdapter<Uri>(getActivity(), android.R.layout.simple_list_item_1, array_list);
        lv = (ListView) v.findViewById(R.id.list_ai);
        lv.setAdapter(array_adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 

                LayoutInflater inflater = getActivity().getLayoutInflater();
                View view = inflater.inflate(R.layout.image_layout, null);

                AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());  
                adb.setView(view);
                adb.show(); 
            }
        });
        return v;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) 
        {           
            case R.id.button_ai:
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, RQS_LOADIMAGE);
            break;
        }           
    }

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

      if (requestCode == RQS_LOADIMAGE) {

          if(resultCode == MainActivity.RESULT_OK) {
            Uri imageUri = data.getData();
            array_list.add(imageUri);
            array_adapter.notifyDataSetChanged();
          }
      }
    }

}

image_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:contentDescription="@string/desc" />


</LinearLayout> 

1 个答案:

答案 0 :(得分:0)

我这样做:
1-创建新活动
2-进入mainfest并将android:theme="@android:style/Theme.Dialog"添加到您的活动中,它将是这样的

<activity
        android:name="package.ShowImage"
        android:label="@string/title_activity_show_image" 
        android:theme="@android:style/Theme.Dialog">
    </activity>

3-转到ShowImage.java并改变它,如果你想这样:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);//hide title of activity
    setContentView(R.layout.image_layout);
}

4-点击
开始此活动 祝你好运

修改
对于你的代码,这是怎么回事

AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());  
adb.setView(view);
Bitmap bmp = BitmapFactory.decodeFile(array_list.get(position).toString());
ImageView image = (ImageView) adb.findViewById(R.id.image);
image.setImageBitmap(bmp); 
adb.show();