我的对话框显示目录/sdcard
及其所有文件。选择文件/图像后,路径与位图图像一起保存,例如
file.name = "/sdcard/Pictures/Screenshots/Screenshot_2013-01-15-10-42-02.jpg";`
但是,每次我尝试通过单击位图打开文件时,都会导致打开的应用程序崩溃,而不是我的应用程序。
我已经设置了写外部存储权限。代码片段如下所示。我真的不知道出了什么问题。我还尝试了所有文件类型,例如txt
, pdf
,doc
等,都导致打开文件应用程序崩溃,并且无法打开文件。
Adapter.java(到位图)
public View getView(int position, View convertView, Viewgroup parent(){
case Image:
Bitmap bp = new BitmapFactory().decodeFile(file.name);
image.setImageBitmap(Bitmap.create(bp, 200, 200, true));
break;
}
活动.java
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.fromFile(new File(file.name)));
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
switch(file.getTypeByName()){
case Image:
intent.setType("image/*");
break;
}
startActivityforResult(intent, 000);
}
日志猫
04-10 12:04:42.164: E/AndroidRuntime(4513): FATAL EXCEPTION: main
04-10 12:04:42.164: E/AndroidRuntime(4513): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.sec.gallery3d/com.android.sec.gallery3d.app.Gallery}: java.lang.NullPointerException
04-10 12:04:42.164: E/AndroidRuntime(4513): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
04-10 12:04:42.164: E/AndroidRuntime(4513): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
04-10 12:04:42.164: E/AndroidRuntime(4513): at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-10 12:04:42.164: E/AndroidRuntime(4513): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
04-10 12:04:42.164: E/AndroidRuntime(4513): at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 12:04:42.164: E/AndroidRuntime(4513): at android.os.Looper.loop(Looper.java:137)
04-10 12:04:42.164: E/AndroidRuntime(4513): at android.app.ActivityThread.main(ActivityThread.java:4512)
04-10 12:04:42.164: E/AndroidRuntime(4513): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 12:04:42.164: E/AndroidRuntime(4513): at java.lang.reflect.Method.invoke(Method.java:511)
04-10 12:04:42.164: E/AndroidRuntime(4513): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
04-10 12:04:42.164: E/AndroidRuntime(4513): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
04-10 12:04:42.164: E/AndroidRuntime(4513): at dalvik.system.NativeStart.main(Native Method)
04-10 12:04:42.164: E/AndroidRuntime(4513): Caused by: java.lang.NullPointerException
04-10 12:04:42.164: E/AndroidRuntime(4513): at com.android.sec.gallery3d.app.Gallery.startViewAction(Gallery.java:377)
04-10 12:04:42.164: E/AndroidRuntime(4513): at com.android.sec.gallery3d.app.Gallery.initializeByIntent(Gallery.java:237)
04-10 12:04:42.164: E/AndroidRuntime(4513): at com.android.sec.gallery3d.app.Gallery.onCreate(Gallery.java:149)
04-10 12:04:42.164: E/AndroidRuntime(4513): at android.app.Activity.performCreate(Activity.java:4469)
04-10 12:04:42.164: E/AndroidRuntime(4513): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
04-10 12:04:42.164: E/AndroidRuntime(4513): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
04-10 12:04:42.164: E/AndroidRuntime(4513): ... 11 more
04-10 12:04:42.164: W/ActivityManager(668): Force finishing activity r.intent.getComponent().flattenToShortString()
04-10 12:04:42.234: E/android.os.Debug(668): !@Dumpstate > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error
04-10 12:04:42.244: I/dumpstate(4530): begin