我试着列出Android设备的所有图像,自14个小时了他们的道路,但我不能。它不断给我了java.lang.RuntimeException:无法启动活动ComponentInfo。误差
我不明白为什么。
任何帮助?
包com.example.allpicsexample;
进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.database.Cursor;
进口android.net.Uri;
进口android.os.Bundle;
进口android.provider.MediaStore;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.BaseAdapter;
进口android.widget.GridView;
进口android.widget.ImageView;
进口android.widget.AdapterView.OnItemClickListener;
/ ** *从SD卡显示图像。 * /公共类MainActivity延伸活动{
/ **
*光标用于从查询的SD卡上的图像访问结果。
* /
私人光标指针;
/ *
*为缩略图图像的ID列索引。
* /
私人诠释参数:columnIndex;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
串文件路径= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
的System.out.println(路径是:+文件路径);
}
}
和这里的LogCat中的信息:
11-14 08:29:01.739:D / AndroidRuntime(16788):关闭VM 11-14 08:29:01.739:W / dalvikvm(16788):主题ID = 1:螺纹退出与 未捕获的异常(组= 0x40018578)8月11号至一十四日:29:01.749: E / AndroidRuntime(16788):致命异常:主要8月11日至一十四日:29:01.749: E / AndroidRuntime(16788):java.lang.RuntimeException的:无法启动 活动 ComponentInfo {com.example.allpicsexample / com.example.allpicsexample.MainActivity}: 显示java.lang.NullPointerException 8月11日至一十四日:29:01.749: E / AndroidRuntime(16788):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 8月11日至14日:29:01.749:E / AndroidRuntime(16788):在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 8月11日至14日:29:01.749:E / AndroidRuntime(16788):在 android.app.ActivityThread.access $ 1500(ActivityThread.java:117)11-14 08:29:01.749:E / AndroidRuntime(16788):在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935) 8月11日至14日:29:01.749:E / AndroidRuntime(16788):在 android.os.Handler.dispatchMessage(Handler.java:99)11-14 08:29:01.749:E / AndroidRuntime(16788):在 android.os.Looper.loop(Looper.java:130)八月11日至十四日:29:01.749: E / AndroidRuntime(16788):在 android.app.ActivityThread.main(ActivityThread.java:3687)11-14 08:29:01.749:E / AndroidRuntime(16788):在 java.lang.reflect.Method.invokeNative(本机方法)11-14 08:29:01.749:E / AndroidRuntime(16788):在 java.lang.reflect.Method.invoke(Method.java:507)11月14日08:29:01.749: E / AndroidRuntime(16788):在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867) 8月11日至14日:29:01.749:E / AndroidRuntime(16788):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)11-14 08:29:01.749:E / AndroidRuntime(16788):在 dalvik.system.NativeStart.main(本机方法)8月十一号至14日:29:01.749: E / AndroidRuntime(16788):由:显示java.lang.NullPointerException 8月11日至14日:29:01.749:E / AndroidRuntime(16788):在 com.example.allpicsexample.MainActivity.onCreate(MainActivity.java:37) 8月11日至14日:29:01.749:E / AndroidRuntime(16788):在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 8月11日至14日:29:01.749:E / AndroidRuntime(16788):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 8月11日至14日:29:01.749:E / AndroidRuntime(16788):...... 11多个
下面是实施Abhishek的code第二崩溃日志后
11-14 10:00:20.159:D / AndroidRuntime(19266):关闭VM 11-14 10:00:20.159:W / dalvikvm(19266):主题ID = 1:螺纹退出与 未捕获的异常(组= 0x40018578)10月11日至十四日:00:20.189: E / AndroidRuntime(19266):致命异常:主要的10月11日至一十四号:00:20.189: E / AndroidRuntime(19266):android.app.SuperNotCalledException:活动 {com.example.allpicsexample / com.example.allpicsexample.MainActivity} 没叫过到super.onCreate()10月11日至一十四号:00:20.189: E / AndroidRuntime(19266):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1617) 10月11号至十四号:00:20.189:E / AndroidRuntime(19266):在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 10月11号至十四号:00:20.189:E / AndroidRuntime(19266):在 android.app.ActivityThread.access $ 1500(ActivityThread.java:117)11-14 10:00:20.189:E / AndroidRuntime(19266):在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935) 10月11号至十四号:00:20.189:E / AndroidRuntime(19266):在 android.os.Handler.dispatchMessage(Handler.java:99)11-14 10:00:20.189:E / AndroidRuntime(19266):在 android.os.Looper.loop(Looper.java:130)10月11号至十四日:00:20.189: E / AndroidRuntime(19266):在 android.app.ActivityThread.main(ActivityThread.java:3687)11-14 10:00:20.189:E / AndroidRuntime(19266):在 java.lang.reflect.Method.invokeNative(本机方法)11-14 10:00:20.189:E / AndroidRuntime(19266):在 java.lang.reflect.Method.invoke(Method.java:507)11月14日10:00:20.189: E / AndroidRuntime(19266):在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867) 10月11号至十四号:00:20.189:E / AndroidRuntime(19266):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)11-14 10:00:20.189:E / AndroidRuntime(19266):在 dalvik.system.NativeStart.main(本机方法)
解决方案您所得到的异常,因为你没有初始化光标
对象。
您可以使用下面的code让所有图像路径。
最终的String []列= {MediaStore.Images.Media.DATA,MediaStore.Images.Media._ID};
最后弦乐排序依据= MediaStore.Images.Media._ID;
//存储所有从游标图库中的图片
光标光标= getContentResolver()查询(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,列,空,
空,排序依据);
//图像总数
诠释计数= cursor.getCount();
//创建一个数组来存储路径的所有图片
的String [] arrPath =新的String [统计]
的for(int i = 0; I<计数;我++){
cursor.moveToPosition(ⅰ);
INT dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
//存储图像的路径
arrPath [I] = cursor.getString(dataColumnIndex);
Log.i(PATH,arrPath [I]);
}
I was trying to list all the images in android device with their paths since 14 hours straight but I wasn't able to .. It keeps giving me "java.lang.RuntimeException: Unable to start activity ComponentInfo" error.
I don't understand why..
Any help??
package com.example.allpicsexample;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
/** * Displays images from an SD card. */ public class MainActivity extends Activity {
/**
* Cursor used to access the results from querying for images on the SD card.
*/
private Cursor cursor;
/*
* Column index for the Thumbnails Image IDs.
*/
private int columnIndex;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
System.out.println("the path is : "+filePath);
}
}
and here is the LogCat info:
11-14 08:29:01.739: D/AndroidRuntime(16788): Shutting down VM 11-14 08:29:01.739: W/dalvikvm(16788): threadid=1: thread exiting with uncaught exception (group=0x40018578) 11-14 08:29:01.749: E/AndroidRuntime(16788): FATAL EXCEPTION: main 11-14 08:29:01.749: E/AndroidRuntime(16788): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.allpicsexample/com.example.allpicsexample.MainActivity}: java.lang.NullPointerException 11-14 08:29:01.749: E/AndroidRuntime(16788): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 11-14 08:29:01.749: E/AndroidRuntime(16788): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 11-14 08:29:01.749: E/AndroidRuntime(16788): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 11-14 08:29:01.749: E/AndroidRuntime(16788): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 11-14 08:29:01.749: E/AndroidRuntime(16788): at android.os.Handler.dispatchMessage(Handler.java:99) 11-14 08:29:01.749: E/AndroidRuntime(16788): at android.os.Looper.loop(Looper.java:130) 11-14 08:29:01.749: E/AndroidRuntime(16788): at android.app.ActivityThread.main(ActivityThread.java:3687) 11-14 08:29:01.749: E/AndroidRuntime(16788): at java.lang.reflect.Method.invokeNative(Native Method) 11-14 08:29:01.749: E/AndroidRuntime(16788): at java.lang.reflect.Method.invoke(Method.java:507) 11-14 08:29:01.749: E/AndroidRuntime(16788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 11-14 08:29:01.749: E/AndroidRuntime(16788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 11-14 08:29:01.749: E/AndroidRuntime(16788): at dalvik.system.NativeStart.main(Native Method) 11-14 08:29:01.749: E/AndroidRuntime(16788): Caused by: java.lang.NullPointerException 11-14 08:29:01.749: E/AndroidRuntime(16788): at com.example.allpicsexample.MainActivity.onCreate(MainActivity.java:37) 11-14 08:29:01.749: E/AndroidRuntime(16788): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-14 08:29:01.749: E/AndroidRuntime(16788): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 11-14 08:29:01.749: E/AndroidRuntime(16788): ... 11 more
Here is the 2nd crash log after implementing Abhishek's code
11-14 10:00:20.159: D/AndroidRuntime(19266): Shutting down VM 11-14 10:00:20.159: W/dalvikvm(19266): threadid=1: thread exiting with uncaught exception (group=0x40018578) 11-14 10:00:20.189: E/AndroidRuntime(19266): FATAL EXCEPTION: main 11-14 10:00:20.189: E/AndroidRuntime(19266): android.app.SuperNotCalledException: Activity {com.example.allpicsexample/com.example.allpicsexample.MainActivity} did not call through to super.onCreate() 11-14 10:00:20.189: E/AndroidRuntime(19266): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1617) 11-14 10:00:20.189: E/AndroidRuntime(19266): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 11-14 10:00:20.189: E/AndroidRuntime(19266): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 11-14 10:00:20.189: E/AndroidRuntime(19266): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 11-14 10:00:20.189: E/AndroidRuntime(19266): at android.os.Handler.dispatchMessage(Handler.java:99) 11-14 10:00:20.189: E/AndroidRuntime(19266): at android.os.Looper.loop(Looper.java:130) 11-14 10:00:20.189: E/AndroidRuntime(19266): at android.app.ActivityThread.main(ActivityThread.java:3687) 11-14 10:00:20.189: E/AndroidRuntime(19266): at java.lang.reflect.Method.invokeNative(Native Method) 11-14 10:00:20.189: E/AndroidRuntime(19266): at java.lang.reflect.Method.invoke(Method.java:507) 11-14 10:00:20.189: E/AndroidRuntime(19266): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 11-14 10:00:20.189: E/AndroidRuntime(19266): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 11-14 10:00:20.189: E/AndroidRuntime(19266): at dalvik.system.NativeStart.main(Native Method)
解决方案
You are getting the exception because you have not initialized cursor
object.
You can use the below code to get path of all the images.
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
//Stores all the images from the gallery in Cursor
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
//Total number of images
int count = cursor.getCount();
//Create an array to store path to all the images
String[] arrPath = new String[count];
for (int i = 0; i < count; i++) {
cursor.moveToPosition(i);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
//Store the path of the image
arrPath[i]= cursor.getString(dataColumnIndex);
Log.i("PATH", arrPath[i]);
}