列出视频 android,代码错误
list videos android, code error
我尝试列出 sdcard 中的所有电影。一切都很好,但应用程序停止了。我在日志中找不到错误。
这个想法是要有一个本地电影列表,但不能让它发挥作用。
请帮忙!
非常感谢
代码:
public class main extends Activity {
//set constants for MediaStore to query, and show videos
private final static Uri MEDIA_EXTERNAL_CONTENT_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
private final static String _ID = MediaStore.Video.Media._ID;
private final static String MEDIA_DATA = MediaStore.Video.Media.DATA;
//flag for which one is used for images selection
private GridView _gallery;
private Cursor _cursor;
private int _columnIndex;
private int[] _videosId;
private Uri _contentUri;
String filename;
int flag = 0;
protected Context _context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_context = getApplicationContext();
setContentView(R.layout.activity_main);
//set GridView for gallery
_gallery = (GridView) findViewById(R.id.videoGrdVw);
//set default as external/sdcard uri
_contentUri = MEDIA_EXTERNAL_CONTENT_URI;
initVideosId();
//set gallery adapter
setGalleryAdapter();
}
private void setGalleryAdapter() {
_gallery.setAdapter(new VideoGalleryAdapter(_context));
_gallery.setOnItemClickListener(_itemClickLis);
flag = 1;
}
private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener()
{
@SuppressWarnings({ "deprecation", "unused", "rawtypes" })
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// Now we want to actually get the data location of the file
String [] proj={MEDIA_DATA};
// We request our cursor again
_cursor = managedQuery(_contentUri,
proj, // Which columns to return
MEDIA_DATA + " like ? ", // WHERE clause; which rows to return (all rows)
new String[] {"%Movies%"}, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
// We want to get the column index for the data uri
int count = _cursor.getCount();
//
_cursor.moveToFirst();
//
_columnIndex = _cursor.getColumnIndex(MEDIA_DATA);
// Lets move to the selected item in the cursor
_cursor.moveToPosition(position);
// And here we get the filename
filename = _cursor.getString(_columnIndex);
//*********** You can do anything when you know the file path :-)
showToast(filename);
Intent i = new Intent(main.this, Player.class);
i.putExtra("videoPath", filename);
startActivity(i);
//
}
};
@SuppressWarnings("deprecation")
private void initVideosId() {
try
{
//Here we set up a string array of the thumbnail ID column we want to get back
String [] proj={_ID};
// Now we create the cursor pointing to the external thumbnail store
_cursor = managedQuery(_contentUri,
proj, // Which columns to return
MEDIA_DATA + " like ? ", // WHERE clause; which rows to return (all rows)
new String[] {"%Movies%"}, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int count= _cursor.getCount();
// We now get the column index of the thumbnail id
_columnIndex = _cursor.getColumnIndex(_ID);
//initialize
_videosId = new int[count];
//move position to first element
_cursor.moveToFirst();
for(int i=0;i<count;i++)
{
int id = _cursor.getInt(_columnIndex);
//
_videosId[i]= id;
//
_cursor.moveToNext();
//
}
}catch(Exception ex)
{
showToast(ex.getMessage().toString());
}
}
protected void showToast(String msg)
{
Toast.makeText(_context, msg, Toast.LENGTH_LONG).show();
}
//
private class VideoGalleryAdapter extends BaseAdapter
{
public VideoGalleryAdapter(Context c)
{
_context = c;
}
public int getCount()
{
return _videosId.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imgVw= new ImageView(_context);;
try
{
if(convertView!=null)
{
imgVw= (ImageView) convertView;
}
imgVw.setImageBitmap(getImage(_videosId[position]));
imgVw.setLayoutParams(new GridView.LayoutParams(200, 200));
imgVw.setPadding(8, 8, 8, 8);
}
catch(Exception ex)
{
System.out.println("main:getView()-135: ex " + ex.getClass() +", "+ ex.getMessage());
}
return imgVw;
}
// Create the thumbnail on the fly
private Bitmap getImage(int id) {
Bitmap thumb = MediaStore.Video.Thumbnails.getThumbnail(
getContentResolver(),
id, MediaStore.Video.Thumbnails.MICRO_KIND, null);
return thumb;
}
}
日志:
> > 01-30 14:08:32.241 11542-11542/ro.vrt.main I/Process: Sending signal. PID: 11542 SIG: 9 01-30 14:08:37.508 11671-11671/ro.vrt.main
> W/System: ClassLoader referenced unknown path:
> /data/app/ro.vrt.main-2/lib/x86 01-30 14:08:37.510
> 11671-11671/ro.vrt.main I/fd: BootstrapApplication created. Android
> package is ro.vrt.main, real application class is null. 01-30
> 14:08:37.511 11671-11671/ro.vrt.main I/fd: Resource override is
> /data/data/ro.vrt.main/files/studio-fd/right/resources.ap_ 01-30
> 14:08:37.511 11671-11671/ro.vrt.main I/fd: Resource patch last
> modified: 1454178687000 01-30 14:08:37.511 11671-11671/ro.vrt.main
> I/fd: APK last modified: 1454178303000 < resource patch 01-30
> 14:08:37.512 11671-11671/ro.vrt.main I/fd: Last code patch:
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_7_2c7a21c06f0b2310eadc38962f037e8d9bfeb1d7-classes.dex
> 01-30 14:08:37.512 11671-11671/ro.vrt.main I/fd: APK last modified:
> 1454178303000 < 1454178752000 code patch 01-30 14:08:37.512
> 11671-11671/ro.vrt.main I/fd: Bootstrapping class loader with dex list
> [/data/data/ro.vrt.main/files/studio-fd/dex/slice_7_2c7a21c06f0b2310eadc38962f037e8d9bfeb1d7-classes.dex,
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_5_19eb0e3297ec4866c77c2a0621402c73a2c55731-classes.dex,
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_3_c9e402f5905d7d75403d2e682e8e8f0378a74371-classes.dex]
> 01-30 14:08:37.512 11671-11671/ro.vrt.main I/fd: Incremental dex path
> is
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_7_2c7a21c06f0b2310eadc38962f037e8d9bfeb1d7-classes.dex:/data/data/ro.vrt.main/files/studio-fd/dex/slice_5_19eb0e3297ec4866c77c2a0621402c73a2c55731-classes.dex:/data/data/ro.vrt.main/files/studio-fd/dex/slice_3_c9e402f5905d7d75403d2e682e8e8f0378a74371-classes.dex
> 01-30 14:08:37.525 11671-11671/ro.vrt.main W/System: ClassLoader
> referenced unknown path: /data/data/ro.vrt.main/files/studio-fd/lib
> 01-30 14:08:37.529 11671-11671/ro.vrt.main I/fd: Starting server
> socket listening for package ro.vrt.main on
> android.net.LocalSocketAddress@7d592ee 01-30 14:08:37.533
> 11671-11671/ro.vrt.main I/fd: Started server for package ro.vrt.main
> 01-30 14:08:37.692 11671-11671/ro.vrt.main D/AndroidRuntime: Shutting
> down VM
>
>
> --------- beginning of crash 01-30 14:08:37.692 11671-11671/ro.vrt.main
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: ro.vrt.main, PID: 11671
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{ro.vrt.main/ro.vrt.main.main}:
> java.lang.NullPointerException: Attempt to get length of null array
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
> at android.app.ActivityThread.-wrap11(ActivityThread.java)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:148)
> at android.app.ActivityThread.main(ActivityThread.java:5417)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
> Caused by: java.lang.NullPointerException: Attempt to get length of null
> array
> at ro.vrt.main.main$VideoGalleryAdapter.getCount(main.java:142)
> at android.widget.GridView.setAdapter(GridView.java:206)
> at ro.vrt.main.main.setGalleryAdapter(main.java:56)
> at ro.vrt.main.main.onCreate(main.java:51)
> at android.app.Activity.performCreate(Activity.java:6237)
> at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
> at android.app.ActivityThread.-wrap11(ActivityThread.java)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:148)
> at android.app.ActivityThread.main(ActivityThread.java:5417)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 01-30
> 14:08:44.290 11671-11671/ro.vrt.main I/Process: Sending signal. PID:
> 11671 SIG: 9 01-30 14:09:23.786 11878-11878/ro.vrt.main I/Process:
> Sending signal. PID: 11878 SIG: 9
和main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="@+id/videoGrdVw"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dip"
android:horizontalSpacing="5dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</GridView>
</LinearLayout>
当您当时将适配器分配给您的列表视图时,_videosId
为空,因为那时数据库查询尚未完成。数据准备好后,您需要设置适配器。
因为你事先并不知道数组的大小;你甚至不能用空白数据初始化数组。
解决方案
从方法 onCreate
中删除 setGalleryAdapter();
和
在方法 initVideosId()
的 for
循环之后放置 setGalleryAdapter();
我尝试列出 sdcard 中的所有电影。一切都很好,但应用程序停止了。我在日志中找不到错误。 这个想法是要有一个本地电影列表,但不能让它发挥作用。 请帮忙! 非常感谢
代码:
public class main extends Activity {
//set constants for MediaStore to query, and show videos
private final static Uri MEDIA_EXTERNAL_CONTENT_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
private final static String _ID = MediaStore.Video.Media._ID;
private final static String MEDIA_DATA = MediaStore.Video.Media.DATA;
//flag for which one is used for images selection
private GridView _gallery;
private Cursor _cursor;
private int _columnIndex;
private int[] _videosId;
private Uri _contentUri;
String filename;
int flag = 0;
protected Context _context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_context = getApplicationContext();
setContentView(R.layout.activity_main);
//set GridView for gallery
_gallery = (GridView) findViewById(R.id.videoGrdVw);
//set default as external/sdcard uri
_contentUri = MEDIA_EXTERNAL_CONTENT_URI;
initVideosId();
//set gallery adapter
setGalleryAdapter();
}
private void setGalleryAdapter() {
_gallery.setAdapter(new VideoGalleryAdapter(_context));
_gallery.setOnItemClickListener(_itemClickLis);
flag = 1;
}
private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener()
{
@SuppressWarnings({ "deprecation", "unused", "rawtypes" })
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// Now we want to actually get the data location of the file
String [] proj={MEDIA_DATA};
// We request our cursor again
_cursor = managedQuery(_contentUri,
proj, // Which columns to return
MEDIA_DATA + " like ? ", // WHERE clause; which rows to return (all rows)
new String[] {"%Movies%"}, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
// We want to get the column index for the data uri
int count = _cursor.getCount();
//
_cursor.moveToFirst();
//
_columnIndex = _cursor.getColumnIndex(MEDIA_DATA);
// Lets move to the selected item in the cursor
_cursor.moveToPosition(position);
// And here we get the filename
filename = _cursor.getString(_columnIndex);
//*********** You can do anything when you know the file path :-)
showToast(filename);
Intent i = new Intent(main.this, Player.class);
i.putExtra("videoPath", filename);
startActivity(i);
//
}
};
@SuppressWarnings("deprecation")
private void initVideosId() {
try
{
//Here we set up a string array of the thumbnail ID column we want to get back
String [] proj={_ID};
// Now we create the cursor pointing to the external thumbnail store
_cursor = managedQuery(_contentUri,
proj, // Which columns to return
MEDIA_DATA + " like ? ", // WHERE clause; which rows to return (all rows)
new String[] {"%Movies%"}, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int count= _cursor.getCount();
// We now get the column index of the thumbnail id
_columnIndex = _cursor.getColumnIndex(_ID);
//initialize
_videosId = new int[count];
//move position to first element
_cursor.moveToFirst();
for(int i=0;i<count;i++)
{
int id = _cursor.getInt(_columnIndex);
//
_videosId[i]= id;
//
_cursor.moveToNext();
//
}
}catch(Exception ex)
{
showToast(ex.getMessage().toString());
}
}
protected void showToast(String msg)
{
Toast.makeText(_context, msg, Toast.LENGTH_LONG).show();
}
//
private class VideoGalleryAdapter extends BaseAdapter
{
public VideoGalleryAdapter(Context c)
{
_context = c;
}
public int getCount()
{
return _videosId.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imgVw= new ImageView(_context);;
try
{
if(convertView!=null)
{
imgVw= (ImageView) convertView;
}
imgVw.setImageBitmap(getImage(_videosId[position]));
imgVw.setLayoutParams(new GridView.LayoutParams(200, 200));
imgVw.setPadding(8, 8, 8, 8);
}
catch(Exception ex)
{
System.out.println("main:getView()-135: ex " + ex.getClass() +", "+ ex.getMessage());
}
return imgVw;
}
// Create the thumbnail on the fly
private Bitmap getImage(int id) {
Bitmap thumb = MediaStore.Video.Thumbnails.getThumbnail(
getContentResolver(),
id, MediaStore.Video.Thumbnails.MICRO_KIND, null);
return thumb;
}
}
日志:
> > 01-30 14:08:32.241 11542-11542/ro.vrt.main I/Process: Sending signal. PID: 11542 SIG: 9 01-30 14:08:37.508 11671-11671/ro.vrt.main
> W/System: ClassLoader referenced unknown path:
> /data/app/ro.vrt.main-2/lib/x86 01-30 14:08:37.510
> 11671-11671/ro.vrt.main I/fd: BootstrapApplication created. Android
> package is ro.vrt.main, real application class is null. 01-30
> 14:08:37.511 11671-11671/ro.vrt.main I/fd: Resource override is
> /data/data/ro.vrt.main/files/studio-fd/right/resources.ap_ 01-30
> 14:08:37.511 11671-11671/ro.vrt.main I/fd: Resource patch last
> modified: 1454178687000 01-30 14:08:37.511 11671-11671/ro.vrt.main
> I/fd: APK last modified: 1454178303000 < resource patch 01-30
> 14:08:37.512 11671-11671/ro.vrt.main I/fd: Last code patch:
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_7_2c7a21c06f0b2310eadc38962f037e8d9bfeb1d7-classes.dex
> 01-30 14:08:37.512 11671-11671/ro.vrt.main I/fd: APK last modified:
> 1454178303000 < 1454178752000 code patch 01-30 14:08:37.512
> 11671-11671/ro.vrt.main I/fd: Bootstrapping class loader with dex list
> [/data/data/ro.vrt.main/files/studio-fd/dex/slice_7_2c7a21c06f0b2310eadc38962f037e8d9bfeb1d7-classes.dex,
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_5_19eb0e3297ec4866c77c2a0621402c73a2c55731-classes.dex,
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_3_c9e402f5905d7d75403d2e682e8e8f0378a74371-classes.dex]
> 01-30 14:08:37.512 11671-11671/ro.vrt.main I/fd: Incremental dex path
> is
> /data/data/ro.vrt.main/files/studio-fd/dex/slice_7_2c7a21c06f0b2310eadc38962f037e8d9bfeb1d7-classes.dex:/data/data/ro.vrt.main/files/studio-fd/dex/slice_5_19eb0e3297ec4866c77c2a0621402c73a2c55731-classes.dex:/data/data/ro.vrt.main/files/studio-fd/dex/slice_3_c9e402f5905d7d75403d2e682e8e8f0378a74371-classes.dex
> 01-30 14:08:37.525 11671-11671/ro.vrt.main W/System: ClassLoader
> referenced unknown path: /data/data/ro.vrt.main/files/studio-fd/lib
> 01-30 14:08:37.529 11671-11671/ro.vrt.main I/fd: Starting server
> socket listening for package ro.vrt.main on
> android.net.LocalSocketAddress@7d592ee 01-30 14:08:37.533
> 11671-11671/ro.vrt.main I/fd: Started server for package ro.vrt.main
> 01-30 14:08:37.692 11671-11671/ro.vrt.main D/AndroidRuntime: Shutting
> down VM
>
>
> --------- beginning of crash 01-30 14:08:37.692 11671-11671/ro.vrt.main
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: ro.vrt.main, PID: 11671
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{ro.vrt.main/ro.vrt.main.main}:
> java.lang.NullPointerException: Attempt to get length of null array
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
> at android.app.ActivityThread.-wrap11(ActivityThread.java)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:148)
> at android.app.ActivityThread.main(ActivityThread.java:5417)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
> Caused by: java.lang.NullPointerException: Attempt to get length of null
> array
> at ro.vrt.main.main$VideoGalleryAdapter.getCount(main.java:142)
> at android.widget.GridView.setAdapter(GridView.java:206)
> at ro.vrt.main.main.setGalleryAdapter(main.java:56)
> at ro.vrt.main.main.onCreate(main.java:51)
> at android.app.Activity.performCreate(Activity.java:6237)
> at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
> at android.app.ActivityThread.-wrap11(ActivityThread.java)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
> at android.os.Handler.dispatchMessage(Handler.java:102)
> at android.os.Looper.loop(Looper.java:148)
> at android.app.ActivityThread.main(ActivityThread.java:5417)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 01-30
> 14:08:44.290 11671-11671/ro.vrt.main I/Process: Sending signal. PID:
> 11671 SIG: 9 01-30 14:09:23.786 11878-11878/ro.vrt.main I/Process:
> Sending signal. PID: 11878 SIG: 9
和main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="@+id/videoGrdVw"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dip"
android:horizontalSpacing="5dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</GridView>
</LinearLayout>
当您当时将适配器分配给您的列表视图时,_videosId
为空,因为那时数据库查询尚未完成。数据准备好后,您需要设置适配器。
因为你事先并不知道数组的大小;你甚至不能用空白数据初始化数组。
解决方案
从方法 onCreate
setGalleryAdapter();
和
在方法 initVideosId()
for
循环之后放置 setGalleryAdapter();