实现一个功能,上报信息,可以添加视频、图片、录音。调用系统相机,所以每次只能选择一张图片。
使用的图片处理方式
http://www.trinea.cn/android/android-imagecache/
//拍照
add_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (commitId.equals("0")) {
Toast.makeText(getApplicationContext(), "请先保存上面的信息", 0).show();
return;
}else{
Intent local = new Intent();
local.setType("image/*");
local.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(local, 11);
}
}
});
//录像
add_video.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (commitId.equals("0")) {
Toast.makeText(getApplicationContext(), "请先保存上面的信息", 0).show();
return;
}else{
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
startActivityForResult(intent, 10);
}
}
});
拍照和视频的处理
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode== Activity.RESULT_OK){
switch(requestCode) {
case 10:
Uri uriVideo = data.getData();
Cursor cursors=this.getContentResolver().query(uriVideo, null, null, null, null);
if (cursors.moveToNext()) {
/* _data:文件的绝对路径 ,_display_name:文件名 */
videoPath = cursors.getString(cursors.getColumnIndex("_data"));
Toast.makeText(this, videoPath, Toast.LENGTH_SHORT).show();
//保存视频路径
imagelist.add(videoPath);
setImageGrideValue();
}
break;
case 11:
Uri uri = data.getData();
Log.e(TAG, "uri = " + uri);
try {
String[] pojo = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, pojo, null, null, null);
if (cursor != null) {
ContentResolver cr = this.getContentResolver();
int colunm_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(colunm_index);
/***
* 这里加这样一个判断主要是为了第三方的软件选择,比如:使用第三方的文件管理器的话,你选择的文件就不一定是图片了,
* 这样的话,我们判断文件的后缀名 如果是图片格式的话,那么才可以
*/
if (path.endsWith("jpg") || path.endsWith("png")) {
srcPath=path;
//保存图片路径
imagelist.add(srcPath);
setImageGrideValue();
} else {
alert();
}
} else {
alert();
}
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void alert() {
Dialog dialog = new AlertDialog.Builder(this).setTitle("提示")
.setMessage("您选择的不是有效的图片")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
srcPath = null;
}
}).create();
dialog.show();
}
在页面显示添加的视频和照片列表(视频使用的是默认图片显示)
@AbIocView(id=R.id.gridView_image_report)
GridView gridView_image_report;
private ArrayList imagelist=new ArrayList();
private void setImageGrideValue() {
setImageListener();
MAImagedapter mAdapter = new MAImagedapter(context);
gridView_image_report.setAdapter(mAdapter);
LayoutParams params = new LayoutParams(mAdapter.getCount() * (200 + 10),
LayoutParams.WRAP_CONTENT);
gridView_image_report.setLayoutParams(params);
gridView_image_report.setColumnWidth(200);
gridView_image_report.setHorizontalSpacing(10);
gridView_image_report.setStretchMode(GridView.NO_STRETCH);
gridView_image_report.setNumColumns(mAdapter.getCount());
}
private void setImageListener() {
gridView_image_report.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Log.e(TAG, "position = " + position);
String url=imagelist.get(position);
// Intent intent = new Intent(AddZhiliangReportActivity.this, PicturePreviewActivity.class);
// intent.putExtra("url", url);
// startActivity(intent);
}
});
//长按删除
gridView_image_report.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> arg0, View arg1,
int arg2, long arg3) {
imagelist.remove(arg2);
setImageGrideValue();
return false;
}
});
}
class MAImagedapter extends BaseAdapter {
Context mContext;
LayoutInflater mInflater;
public MAImagedapter(Context c) {
mContext = c;
mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imagelist.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View contentView, ViewGroup arg2) {
String image=imagelist.get(position);
//视频默认图片
if (image.contains("3gp")||image.contains("mp4")) {
image=URLs.URL_API_HOST+"public/images/video_play_btn.png";
}
Log.e(TAG, image);
ViewHolder holder;
if (contentView == null) {
holder = new ViewHolder();
contentView = mInflater.inflate(R.layout.gridview_item, null);
holder.mImg = (ImageView) contentView.findViewById(R.id.mImage);
Bitmap bitmap=ImageLoader.getInstance().loadImage(image, 200, 100, new OnCallBackListener() {
@Override
public void setOnCallBackListener(Bitmap bitmap, String url) {
ImageView image=(ImageView) gridView_image_report.findViewWithTag(url);
if(image!=null&&bitmap!=null){
image.setImageBitmap(bitmap);
}
}
});
if(bitmap!=null){
holder.mImg.setImageBitmap(bitmap);
}else{
holder.mImg.setImageResource(R.drawable.friends_sends_pictures_no);
}
} else {
holder = (ViewHolder) contentView.getTag();
}
contentView.setTag(holder);
return contentView;
}
}
static class ViewHolder {
ImageView mImg;
}
gridview_item.xml
<RelativeLayout android:id="@+id/widget29"
android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/mImage"
android:layout_width="120dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/image_loading" >
ImageView>
RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<HorizontalScrollView
android:id="@+id/horizontalScrollView_report"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<GridView
android:id="@+id/gridView_image_report"
android:layout_width="match_parent"
android:layout_height="60dp" >
GridView>
LinearLayout>
HorizontalScrollView>
LinearLayout>