转载请注明本文出自Jiaxu的专栏(http://blog.csdn.net/u011510784/article/details/48550037),谢谢!
项目开发中经常遇到选择照片或者拍照并上传到服务器上的情况,本文主要介绍手机中图片的选择及显示,废话不多,有图有真相:
为提高可移植性,在主函数中的代码很简单,只需要初始化GridView即可,MainActivity中:
package com.example.tackphoto; import android.content.Context; import android.os.Bundle; import com.example.showphoto.GridViewForListView; import com.example.showphoto.WriteActivity; public class MainActivity extends WriteActivity { private Context context; private GridViewForListView mGridView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; mGridView = (GridViewForListView) findViewById(R.id.gridView1); setGridViewForListView(context, mGridView); } }
接下来看一下WriteActivity中的setGridViewForListView方法.这里面主要处理图片在GridView上的显示以及GridView的点击事件
public void setGridViewForListView(Context context, GridViewForListView mGridView) { this.mGridView = mGridView; this.context = context; dataForGV = new ArrayList<Bitmap>(); listfile = new ArrayList<String>(); getImageBitmap(); }可以看到,进来后会执行getImageBitmap()方法,在这个方法中会初始化一张添加图片,点击此图片可以进行添加图片操作,在这里面也初始化了GridView的适配器以及点击事件:
if (adapter == null && dataForGV != null) { adapter = new AddPhotoGrideViewAdapter(context, listfile); mGridView.setAdapter(adapter); addPhotoGrideViewOnItemClickListener = new AddPhotoGrideViewOnItemClickListener( context, this, adapter, listfile, SELECTPHOTO, TAKE_PICTURE); mGridView .setOnItemClickListener(addPhotoGrideViewOnItemClickListener); }
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (adapter != null) { if ((adapter.getCount() - 1) == position) { if (listfile.size() >= 6) { Toast.makeText(context, "最多只能添加六张照片!", Toast.LENGTH_LONG) .show(); } else { showmySelectDialog("拍照", "从相册选择"); } } else { dialog(position); } } }
继续看一下选择图片:
@Override public void onClick(View v) { Intent intent = null; switch (v.getId()) { case R.id.tv_one: outputFileUri = getOutputFileUri(); intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); activity.startActivityForResult(intent, takePhotoRequest); break; case R.id.tv_three: intent = new Intent(); intent.setClass(context, ImgFileListActivity.class); // intent.putExtra("isSingled", false); activity.startActivityForResult(intent, addPhotoRequest); break; case R.id.tv_cancel: dismissSelectDialog(); break; } }
/** * 获取全部图片地址 * * @return */ public ArrayList<String> listAlldir() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Uri uri = intent.getData(); ArrayList<String> list = new ArrayList<String>(); String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);// managedQuery(uri, proj, null, null, null); while (cursor.moveToNext()) { String path = cursor.getString(0); list.add(new File(path).getAbsolutePath()); } return list; }
private List<String> getSelectedImgPath() { List<String> imgsPath = getImgPath(); return imgsPath; }
项目源码>>>