Reputation: 91
My dialog displays directory /sdcard
and all its files. Upon selecting the file/image, the path is saved along with the bitmap image, e.g.
file.name = "/sdcard/Pictures/Screenshots/Screenshot_2013-01-15-10-42-02.jpg";`
However, every time i try to open the file by clicking the bitmap, it causes the opening application to crash, not my application.
I have set the write external storage permission. Code snippet as shown below. I have really no idea what's going wrong. I also tried all file types, such as txt
, pdf
, doc
, etc., all causing the open file application to crash, and not opening the file.
Adapter.java (to bitmap)
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;
}
Activity.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);
}
Logcat
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
Upvotes: 0
Views: 382
Reputation: 2183
So I found this: https://stackoverflow.com/a/3677598/1704011 answer
To quote a commenter:
using setDataAndType() instead of the 2 separate calls fixed her issue.
That really confused the hell out me why that would be necessary. Then I looked at the implementation of Intent.setType()
, Intent.setData()
, and Intent.setDataAndType()
. And look at this:
public Intent setType(String type) {
mData = null;
mType = type;
return this;
}
public Intent setData(Uri data) {
mData = data;
mType = null;
return this;
}
So a call to setType
after setData
clobbers the data and a call to setData
clobbers the type. That's why the gallery app is breaking, you setType
after setData
so when the intent is sent the data is null. Granted the docs do mention this behavior, but it is not intuitive based on the method names, something like setTypeOnly
and setDataOnly
seems more explicitly convey the purpose of those methods. Otherwise it is unclear that these methods are mutually exclusive.
Use setDataAndType()
and that should fix your issue.
Upvotes: 1