目前我正在使用listview
创建带有视频缩略图的文件浏览器。但是,createVideoThumbnail
时应用程序崩溃,删除代码后应用程序运行正常。以下是createVideoThumbnail
。
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (!file.isHidden() && file.canRead()) {
path.add(file.getPath());
if (file.isDirectory()) {
item.add(file.getName() + "/");
} else {
item.add(file.getName());
Bitmap bMap = ThumbnailUtils.createVideoThumbnail(file.getPath(),
MediaStore.Video.Thumbnails.MICRO_KIND);
rowImage.setImageBitmap(bMap);
}
}
}
下面是全班源代码
public class Video extends ListActivity {
List<String> item = null;
List<String> path = null;
String root;
TextView myPath;
int visibility;
String VideoPath;
ListView list;
VideoView VideoView;
View v;
File f;
Button home;
Button exit;
ImageView rowImage;
MediaController mController;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
myPath = (TextView) findViewById(R.id.path);
VideoView = (VideoView) findViewById(R.id.videoView);
list = (ListView) findViewById(android.R.id.list);
home = (Button) findViewById(R.id.home);
exit = (Button) findViewById(R.id.exit);
rowImage = (ImageView) findViewById(R.id.rowimage);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
home.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getDir(root);
}
});
exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
void getDir(String dirPath) {
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
f = new File(dirPath);
File[] files = f.listFiles();
if (!dirPath.equals(root)) {
item.add("/BACK");
path.add(f.getParent());
}
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (!file.isHidden() && file.canRead()) {
path.add(file.getPath());
if (file.isDirectory()) {
item.add(file.getName() + "/");
} else {
item.add(file.getName());
Bitmap bMap = ThumbnailUtils.createVideoThumbnail(file.getPath(),
MediaStore.Video.Thumbnails.MICRO_KIND);
rowImage.setImageBitmap(bMap);
}
}
}
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, R.id.rowtext, item);
Collections.sort(path);
Collections.sort(item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(path.get(position));
if (file.isDirectory()) {
if (file.canRead()) {
getDir(path.get(position));
} else {
new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
} else {
if (visibility == 0) {
VideoView.setVisibility(View.VISIBLE);
visibility = 1;
VideoPath = file.getPath();
Uri uri = Uri.parse(VideoPath);
VideoView.setVideoURI(uri);
list.setVisibility(View.INVISIBLE);
mController = new MediaController(this);
VideoView.setMediaController(mController);
VideoView.start();
Toast.makeText(getApplicationContext(), file.getAbsolutePath(), Toast.LENGTH_LONG).show();
Intent intent = getIntent();
String intentAction = intent.getAction();
if (!TextUtils.isEmpty(intentAction) && intentAction.equals(Intent.ACTION_VIEW)) {
VideoPath = intent.getDataString();
}
} else if (visibility == 1) {
VideoView.setVisibility(View.INVISIBLE);
list.setVisibility(View.VISIBLE);
visibility = 0;
VideoView.stopPlayback();
}
}
}
public void onBackPressed() {
if (visibility == 1) {
VideoView.setVisibility(View.INVISIBLE);
list.setVisibility(View.VISIBLE);
VideoView.stopPlayback();
visibility = 0;
} else if (f.getPath().equals(root)) {
finish();
} else {
getDir(f.getParent());
}
}
}
下面的是logcat
12-16 18:28:18.449: D/SurfaceView(7333): get context in surfaceview init
12-16 18:28:21.959: D/AndroidRuntime(7333): Shutting down VM
12-16 18:28:21.959: W/dalvikvm(7333): threadid=1: thread exiting with uncaught exception (group=0x412a8930)
12-16 18:28:21.969: E/AndroidRuntime(7333): FATAL EXCEPTION: main
12-16 18:28:21.969: E/AndroidRuntime(7333): java.lang.NullPointerException
12-16 18:28:21.969: E/AndroidRuntime(7333): at com.myapp.MyCarousel.Video.getDir(Video.java:135)
12-16 18:28:21.969: E/AndroidRuntime(7333): at com.myapp.MyCarousel.Video.onListItemClick(Video.java:188)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.widget.AbsListView.onKeyUp(AbsListView.java:2890)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.widget.ListView.commonKey(ListView.java:2258)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.widget.ListView.onKeyUp(ListView.java:2113)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.view.KeyEvent.dispatch(KeyEvent.java:2865)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.view.View.dispatchKeyEvent(View.java:7205)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1354)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.widget.ListView.dispatchKeyEvent(ListView.java:2088)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
12-16 18:28:21.969: E/AndroidRuntime(7333): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
12-16 18:28:21.969: E/AndroidRuntime(7333): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.app.Activity.dispatchKeyEvent(Activity.java:2394)
12-16 18:28:21.969: E/AndroidRuntime(7333): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3703)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3653)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2820)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.os.Looper.loop(Looper.java:137)
12-16 18:28:21.969: E/AndroidRuntime(7333): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-16 18:28:21.969: E/AndroidRuntime(7333): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 18:28:21.969: E/AndroidRuntime(7333): at java.lang.reflect.Method.invoke(Method.java:511)
12-16 18:28:21.969: E/AndroidRuntime(7333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-16 18:28:21.969: E/AndroidRuntime(7333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-16 18:28:21.969: E/AndroidRuntime(7333): at dalvik.system.NativeStart.main(Native Method)
任何人都有任何想法?谢谢!