列出Android设备上的所有图像

我试图列出Android设备中的所有图像与他们的路径自14小时以来,但我无法......它不断给我“java.lang.RuntimeException:无法启动活动ComponentInfo”错误。

我不明白为什么..

任何帮助?

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);

}
}

这里是LogCat信息:

11-14 08:29:01.739:D / AndroidRuntime(16788):关闭虚拟机11-14 08:29:01.739:W / dalvikvm(16788):threadid = 1:线程以未捕获的异常退出(group = 0x40018578)11 -14 08:29:01.749:E / AndroidRuntime(16788):致命异常:main 11-14 08:29:01.749:E / AndroidRuntime(16788):java.lang.RuntimeException:无法启动活动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) 14 08:29:01.749:E / AndroidRuntime(16788):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)11-14 08:29:01.749:E / AndroidRuntime(16788):在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):在android.os.Handler.dispatchMe ssage(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):在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):引起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更多

这是执行Abhishek代码后的第二次崩溃日志

11-14 10:00:20.159 D / AndroidRuntime(19266):关闭虚拟机11-14 10:00:20.159:W / dalvikvm(19266):threadid = 1:线程退出时未捕获异常(group = 0x40018578)11 -14 10:00:20.189:E / AndroidRuntime(19266):致命例外:main 11-14 10:00:20.189:E / AndroidRuntime(19266):android.app.SuperNotCalledException:Activity {com.example.allpicsexample / com .example.allpicsexample.MainActivity}没有调用super.onCreate()11-14 10:00:20.189:E / AndroidRuntime(19266):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1617) 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(Handl er.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) 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)


你正在得到这个异常,因为你没有初始化cursor对象。 您可以使用下面的代码来获取所有图像的路径。

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]);
        }  
链接地址: http://www.djcxy.com/p/96817.html

上一篇: List all the images on android device

下一篇: Android SQLite SUM, throws NullPointer