2

I want to set an image as contact icon. That image is stored in cache. This is my code -

Intent myIntent = new Intent();
    myIntent.setAction(Intent.ACTION_ATTACH_DATA);
    myIntent.setType("image/jpeg");
    myIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(getApplicationContext().getCacheDir()
            .getAbsolutePath()
            + "/" + fileName));
   startActivityForResult(Intent.createChooser(myIntent, "Set As"),
            200);

This code is giving me option - contact icon, wallpaper. when i select contact icon, contact list is getting open. As i select any contact from contact list the app is getting crashed.

Logs are -

E/AndroidRuntime(15004): FATAL EXCEPTION: main
E/AndroidRuntime(15004): java.lang.RuntimeException: Failure delivering result                     ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r2-5C48544A48463C46323C2C/2 (has extras) }} to activity {com.android.contacts/com.android.contacts.AttachImage}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.camera.action.CROP (has extras) }
E/AndroidRuntime(15004):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
E/AndroidRuntime(15004):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
E/AndroidRuntime(15004):    at android.app.ActivityThread.access$2800(ActivityThread.java:125)
E/AndroidRuntime(15004):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
E/AndroidRuntime(15004):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(15004):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(15004):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(15004):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15004):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(15004):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(15004):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(15004):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(15004): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.camera.action.CROP (has extras) }
E/AndroidRuntime(15004):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
E/AndroidRuntime(15004):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
E/AndroidRuntime(15004):    at android.app.Activity.startActivityForResult(Activity.java:2817)
E/AndroidRuntime(15004):    at com.android.contacts.AttachImage.onActivityResult(AttachImage.java:133)
E/AndroidRuntime(15004):    at android.app.Activity.dispatchActivityResult(Activity.java:3890)
E/AndroidRuntime(15004):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)

I couldn't find whats wrong with my code. Do i need to add some permission for this?

This can be duplicate of android set image as contact icon/wallpaper

But i did not find any solution for this. Please help me if someone knows the solution.

Community
  • 1
  • 1
Shweta
  • 349
  • 1
  • 2
  • 15

2 Answers2

7

I have done it. You just need to set proper intent and the Intent is

Uri sendUri = Uri.fromFile(externalFile)
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
        intent.setDataAndType(sendUri, "image/jpg");
        intent.putExtra("mimeType", "image/jpg");
        startActivityForResult(Intent.createChooser(intent, "Set As"), 200);
Shweta
  • 349
  • 1
  • 2
  • 15
  • I had a similar problem: Contacts and Nova Wallpapers were crashing for me when I sent this intent. I have added `intent.putExtra("mimeType", "image/jpg")` and now they work. Thanks! – Twinsen Jan 07 '14 at 09:43
0

The error is apparent:

Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.camera.action.CROP (has extras) }
E/AndroidRuntime(15004):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)

As can be noted here, nothing can handle that action(com.android.camera.action.CROP), I HOPE you didn't type in that action for that intent.

This may provide some insight: How to select and crop an image in android?

Community
  • 1
  • 1
JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • Thanks @JoxTraex for quick responce. As i mentioned in question that i am setting Intent.ACTION_ATTACH_DATA action. This starts com.android.contacts/.AttachImage component. This activity is setting com.android.camera.action.CROP action. So it should be handled by this activity. Also i am not getting any call after selecting contact from list, so that i can set this action. – Shweta Feb 02 '12 at 13:39
  • Well something is missing. Look at the source. – JoxTraex Feb 02 '12 at 13:41