将缓存图像设置为联系人照片

时间:2012-02-02 13:15:56

标签: android contact

我想将图片设置为联系人图标。该图像存储在缓存中。这是我的代码 -

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

这段代码给了我选项 - 联系人图标,壁纸。 当我选择联系人图标时,联系人列表正在打开。当我从联系人列表中选择任何联系人时,该应用程序正在崩溃。

日志是 -

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)

我找不到我的代码有什么问题。我是否需要为此添加一些权限?

这可能是android set image as contact icon/wallpaper

的副本

但我没有找到任何解决方案。如果有人知道解决方案,请帮助我。

2 个答案:

答案 0 :(得分:7)

我做到了。你只需要设置正确的意图,意图是

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

答案 1 :(得分:0)

错误显而易见:

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)

这里可以注意到,没有任何东西可以处理这个动作( com.android.camera.action.CROP ),我 HOPE 你没有输入那个动作为了那个意图。

这可能会提供一些见解: How to select and crop an image in android?