【问题标题】:List all the images on android device列出 android 设备上的所有图像
【发布时间】:2013-11-26 22:03:11
【问题描述】:

自 14 小时以来,我一直试图列出 android 设备中的所有图像及其路径,但我无法......它一直给我“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):关闭 VM 11-14 08:29:01.739: W/dalvikvm(16788): threadid=1: 线程退出 未捕获的异常(组=0x40018578)11-14 08:29:01.749: E/AndroidRuntime(16788): 致命例外: 主 11-14 08:29:01.749: E/AndroidRuntime(16788): java.lang.RuntimeException: 无法启动 活动 组件信息{com.example.allpicsexample/com.example.allpicsexample.MainActivity}: java.lang.NullPointerException 11-14 08:29:01.749: E/AndroidRuntime(16788):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 11-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): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 11-14 08: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-14 08: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(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):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 11-14 08: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(本机方法)11-14 08:29:01.749: E/AndroidRuntime(16788):原因:java.lang.NullPointerException 11-14 08:29:01.749: E/AndroidRuntime(16788): 在 com.example.allpicsexample.MainActivity.onCreate(MainActivity.java:37) 11-14 08:29:01.749: E/AndroidRuntime(16788): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-14 08:29:01.749: E/AndroidRuntime(16788): 在 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): 关闭 VM 11-14 10:00:20.159: W/dalvikvm(19266): threadid=1: 线程退出 未捕获的异常(组=0x40018578)11-14 10:00:20.189: E/AndroidRuntime(19266): 致命例外: 主 11-14 10:00:20.189: E/AndroidRuntime(19266): android.app.SuperNotCalledException: 活动 {com.example.allpicsexample/com.example.allpicsexample.MainActivity} 没有调用 super.onCreate() 11-14 10:00:20.189: E/AndroidRuntime(19266):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1617) 11-14 10:00:20.189: E/AndroidRuntime(19266): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 11-14 10: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) 11-14 10: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) 11-14 10: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(Native Method) 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) 11-14 10: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(Native Method)

【问题讨论】:

  • 您好,能否提供完整的日志?确保您没有忘记在应用程序清单中声明您的活动。干杯!
  • @k3v1n4ud3 我已经粘贴了完整的 logcat 日志,你现在可以查看.. 谢谢 :)
  • @k3v1n4ud3 你也可以告诉我一种列出安卓设备中所有图像的方法....
  • 我不确定如何访问 SD 卡上的所有内容,您应该查看 developer.android.com/guide/topics/data/…。但是我想我知道它为什么会崩溃,您在尚未初始化的游标对象上调用 getString 。即使您在访问它之前对其进行了初始化,它仍然是空的。尝试替换它,它将防止崩溃。
  • 如果我删除它,那么做任何事情都没有意义..

标签: android image


【解决方案1】:

您收到异常是因为您尚未初始化 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]);
        }  

【讨论】:

  • 我可以使用您的代码使其正常工作,但应用程序在一段时间后崩溃了?
  • @user2990269 你能发布 Logcat 吗?
  • 我已经发布了 LobCat 信息,请查看我帖子中的第二个 logcat。
  • @user2990269 你在onCreate()方法的开头调用了super.onCreate(savedInstanceState);吗?
  • 这拯救了我的一天!非常感谢:)
猜你喜欢
  • 2011-03-19
  • 2015-08-13
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
相关资源
最近更新 更多