第一步,MainActivity.class类代码 如下:
package com.learn.lister.systemvediorecord;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v4.BuildConfig;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements OnItemClickListener {
private static final String TAG = "MainActivity";
//自己本地文件保存路径
//private String video_path = "/storage/emulated/0/Camera/";
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/XiaoGou/";
private List<Picture> listPictures;
ListView listView;
GridView mGridView;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if (msg.what == 0) {
List<Picture> listPictures = (List<Picture>) msg.obj;
MyAdapter adapter = new MyAdapter(listPictures);
mGridView.setAdapter(adapter);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadVaule();
}
private void loadVaule() {
File file = new File(video_path);
//判断文件夹是否存在,如果不存在就创建一个
if (!file.exists()) {
file.mkdirs();
}
File[] files = file.listFiles();
listPictures = new ArrayList<Picture>();
for (int i = 0; i < files.length; i++) {
Picture picture = new Picture();
picture.setBitmap(getVideoThumbnail(files[i].getPath(), 200, 200, MediaStore.Images.Thumbnails.MICRO_KIND));
picture.setPath(files[i].getPath());
picture.setName(files[i].getName());
listPictures.add(picture);
}
// listView = (ListView) findViewById(R.id.lv_show);
mGridView = (GridView) findViewById(R.id.gridView);
mGridView.setOnItemClickListener(this);
Message msg = new Message();
msg.what = 0;
msg.obj = listPictures;
handler.sendMessage(msg);
}
//获取视频的缩略图
private Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) {
Bitmap bitmap = null;
// 获取视频的缩略图
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
// System.out.println("w"+bitmap.getWidth());
// System.out.println("h"+bitmap.getHeight());
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}
public class MyAdapter extends BaseAdapter {
private List<Picture> listPictures;
public MyAdapter(List<Picture> listPictures) {
super();
this.listPictures = listPictures;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listPictures.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listPictures.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View v, ViewGroup arg2) {
// TODO Auto-generated method stu
View view = getLayoutInflater().inflate(R.layout.item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_show);
TextView textView = (TextView) view.findViewById(R.id.tv_show);
imageView.setImageBitmap(listPictures.get(position).getBitmap());
//textView.setText(listPictures.get(position).getPath());
textView.setText(listPictures.get(position).getName());
return view;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
playVideo(listPictures.get(arg2).getPath());
Log.e("path", listPictures.get(arg2).getPath());
}
//调用系统播放器 播放视频
private void playVideo(String videoPath) {
// Intent intent = new Intent(Intent.ACTION_VIEW);
// String strend="";
// if(videoPath.toLowerCase().endsWith(".mp4")){
// strend="mp4";
// }
// else if(videoPath.toLowerCase().endsWith(".3gp")){
// strend="3gp";
// }
// else if(videoPath.toLowerCase().endsWith(".mov")){
// strend="mov";
// }
// else if(videoPath.toLowerCase().endsWith(".avi")){
// strend="avi";
// }
// intent.setDataAndType(Uri.parse(videoPath), "video/*");
// startActivity(intent);
Intent intent = new Intent(Intent.ACTION_VIEW);
//判断是否是AndroidN以及更高的版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID +
".fileProvider", file);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri, "video/*");
} else {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file), "video/*");
}
startActivity(intent);
}
}
第二步:activity_main.xml文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.learn.lister.systemvediorecord.MainActivity">
<GridView
android:id="@+id/gridView"
android:layout_marginTop="10dp"
android:numColumns="2"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</GridView>
</LinearLayout>
第三步:在res目录下创建一个名为 xml 文件夹,在文件中创建一个名为 file_paths.xml 文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!--Android/data/包名/-->
<external-path path="Android/data/com.learn.lister.systemvediorecord/" name="files_root"/>
<external-path path="." name="com.learn.lister.systemvediorecord" />
</paths>
第四步:在AndroidManifest.xml文件中添加权限,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
在 application标签下添加如下代码:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.learn.lister.systemvediorecord.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
注意:上面权限只是读写权限,如果没有反应或有问题,请查看手机设置程序权限是否打开
附加:手动申请打开权限代码
//读写文件权限申请
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 判断是否有这个权限,是返回PackageManager.PERMISSION_GRANTED,否则是PERMISSION_DENIED
// 这里我们要给应用授权所以是!= PackageManager.PERMISSION_GRANTED
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// 如果应用之前请求过此权限但用户拒绝了请求,且没有选择"不再提醒"选项 (后显示对话框解释为啥要这个权限),此方法将返回 true。
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
// requestPermissions以标准对话框形式请求权限。123是识别码(任意设置的整型),用来识别权限。应用无法配置或更改此对话框。
//当应用请求权限时,系统将向用户显示一个对话框。当用户响应时,系统将调用应用的 onRequestPermissionsResult() 方法,向其传递用户响应。您的应用必须替换该方法,以了解是否已获得相应权限。回调会将您传递的相同请求代码传递给 requestPermissions().ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},123);
}
}
} else {
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case 123: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//这里的两句语句是我写的创建文件的语句,在授权成功是时候会调用这里的语句。
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
}
return;
}
}
}