1.在这里上传视频到服务器用的是xutils3.0,当然大家也可以用okhttp进行操作.
第一步:我们就是要从我们app的视频列表,进入我们查询本地视频的那个activity,代码如下:
Intent i = new Intent(SelectVideoActivity.this,
GetVideoActivity.class);
startActivityForResult(i, REQUEST_CODE);
这里用startActivityForResult是为了获取返回选中的本地视频的信息.
第二步:在GetVideoActivity里面查询本地所有视频,并将查询到的视频显示在我们GetVideoActivity的页面上,代码如下:
GetVideoActivity的xml布局代码:
android:id="@+id/lv_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
GetVideoActivity中的代码:
public class GetVideoActivity extends Activity {
private ListView lv;
Context mContext;
private VideodetailAdapter adapter;
private List bit = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get);
lv = (ListView) findViewById(R.id.lv_video);
mContext = this;
new Search_photo().start();
}
private Handler mHandler = new Handler() {
@SuppressLint("ShowToast")
public void handleMessage(android.os.Message msg) {
if (msg.what == 1&&bit!=null) {
adapter = new VideodetailAdapter(mContext, bit);
Toast.makeText(mContext, "视频总数:" + bit.size(), Toast.LENGTH_SHORT).show();
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3) {
if(bit.size()!=0){
Intent intent = new Intent();
// String type = "video/mp4";
// Uri uri = Uri.parse("file://"+bit.get(arg2).getUri());
// intent.setDataAndType(uri, type);
// startActivity(intent);
String uri = bit.get(arg2).getUri();
intent.putExtra("url",bit.get(arg2).getUri());
Log.e("url_ssss", bit.get(arg2).getUri());
setResult(RESULT_OK, intent);
finish();
}
}
});
}
};
};
/**
*
* 遍历系统数据库找出相应的是视频的信息,每找出一条视频信息的同时利用与之关联的找出对应缩略图的uri
* 再异步加载缩略图,
* 由于查询速度非常快,全部查找完成在设置,等待时间不会太长
* @author Administrator
*
*/
class Search_photo extends Thread {
@Override
public void run() {
// 如果有sd卡(外部存储卡)
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
Uri originalUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
ContentResolver cr = mContext.getApplicationContext().getContentResolver();
Cursor cursor = cr.query(originalUri, null, null, null, null);
if (cursor == null) {
return;
}
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
long duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
//获取当前Video对应的Id,然后根据该ID获取其缩略图的uri
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
String[] selectionArgs = new String[] { id + "" };
String[] thumbColumns = new String[] { MediaStore.Video.Thumbnails.DATA,
MediaStore.Video.Thumbnails.VIDEO_ID };
String selection = MediaStore.Video.Thumbnails.VIDEO_ID + "=?";
String uri_thumb = "";
Cursor thumbCursor = (mContext.getApplicationContext().getContentResolver()).query(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, selection, selectionArgs,
null);
if (thumbCursor != null && thumbCursor.moveToFirst()) {
uri_thumb = thumbCursor
.getString(thumbCursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA));
}
BitmapEntity bitmapEntity = new BitmapEntity(title, path, size, uri_thumb, duration);
bit.add(bitmapEntity);
}
if (cursor != null) {
cursor.close();
mHandler.sendEmptyMessage(1);
}
}
}
}
}
第三步:ListView视频列表VideodetailAdapter的item的布局文件代码如下:
第四步:适配器VideodetailAdapter的代码:
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import teacher.arriga.com.arrigateacher.R;
import teacher.arriga.com.arrigateacher.activity.video.BitmapEntity;
import teacher.arriga.com.arrigateacher.activity.video.TimeChange;
/**
* Created by Administrator on 2017/3/7.
*/
@SuppressLint({"InflateParams", "ViewHolder"})
public class VideodetailAdapter extends BaseAdapter {
private List objects = new ArrayList();
@SuppressWarnings("unused")
private Context context;
private LayoutInflater layoutInflater;
public VideodetailAdapter(Context context, List objects) {
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
this.objects = objects;
}
public List setObjects(List objects) {
return this.objects = objects;
}
@Override
public int getCount() {
return objects.size();
}
@Override
public BitmapEntity getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
/**
* 数据量过大可能出现错乱,暂时不用缓存策略
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.videtail_list, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.imgv = (ImageView) convertView.findViewById(R.id.imgv);
viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);
viewHolder.size = (TextView) convertView.findViewById(R.id.video_size);
viewHolder.duration = (TextView) convertView.findViewById(R.id.video_time_long);
convertView.setTag(viewHolder);
// }
initializeViews((BitmapEntity) getItem(position), (ViewHolder) convertView.getTag());
return convertView;
}
private void initializeViews(BitmapEntity object, ViewHolder holder) {
Log.e("adapter", "file://" + object.getUri_thumb());
ImageLoader.getInstance().displayImage("file://" + object.getUri_thumb(), holder.imgv);
holder.tv.setText(object.getName());
holder.size.setText(TimeChange.bytes2kb(object.getSize()) + "");
holder.duration.setText(TimeChange.setTime(object.getDuration()));
}
protected class ViewHolder {
private ImageView imgv;
private TextView tv;
private TextView size;
private TextView duration;
}
}
第五步:这样就查询到了本地所有视频文件信息,显示在列表里,之前我们是通过startActivityForResult()开启的,所以我们可以在onActivityResult获取到返回的选中的视频信息.代码如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE&& resultCode == RESULT_OK && null != data) {
String selectedVideo = (String) data.getExtras().get("url");
File file = new File(selectedVideo);
//准备上传,显示提示文件
upLoadVideo(file);
}
}
第六步:上传视频文件的方法upLoadVideo(File file):
//上传视频文件到服务器
private void upLoadVideo(File file) {
RequestParams params=new RequestParams(HttpUrl.UP_PHOTO);
params.addBodyParameter("fileType", "video");
params.addBodyParameter("filePurpose","broadcastvideo");
params.addBodyParameter("fkPurposeId", id);
params.addParameter("uploadFile",file);
x.http().post(params, new Callback.CommonCallback() {
@Override
public void onSuccess(String result) {
try {
JSONObject jsonObject=new JSONObject(result);
String errcode = jsonObject.optString("errcode");
if ("0".equals(errcode)){
ToastUtil.showToast("上传成功!");
//刷新界面,重新请求数据
queryVideo();
adapter.notifyDataSetChanged();
}else{
ToastUtil.showToast(jsonObject.getString("msg"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.e("4444",ex.getMessage().toString());
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
最后大概整个视频从本地查找,到选择,再到上传到服务器的整个过程就完成了.