将位图转换为Uri

时间:2014-02-24 17:30:57

标签: android email android-intent bitmap uri

我试图通过电子邮件发送图像,但不能在没有错误的情况下执行此操作; \ 我认为问题是我将位图转换为uri的方式,这样:

public Uri getImageUri(Context inContext, Bitmap inImage) {
          ByteArrayOutputStream bytes = new ByteArrayOutputStream();
          inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
          String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
          return Uri.parse(path);
        }

这就是我发送电子邮件的方式:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
             emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ "example@gmail.com"});
             emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "a");
             emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "example@gmail.com");
             emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, getImageUri(this, origBm));
             emailIntent.setType("image/jpeg");

           startActivity(Intent.createChooser(emailIntent, "Send mail..."));

究竟是什么问题?谢谢你的帮助:P 我的错误:

02-24 17:40:27.959: E/DatabaseUtils(291): Writing exception to parcel
02-24 17:40:27.959: E/DatabaseUtils(291): java.lang.UnsupportedOperationException: Unknown URI: content://media/external/images/media
02-24 17:40:27.959: E/DatabaseUtils(291):   at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:1802)
02-24 17:40:27.959: E/DatabaseUtils(291):   at com.android.providers.media.MediaProvider.insert(MediaProvider.java:1744)
02-24 17:40:27.959: E/DatabaseUtils(291):   at android.content.ContentProvider$Transport.insert(ContentProvider.java:198)
02-24 17:40:27.959: E/DatabaseUtils(291):   at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:146)
02-24 17:40:27.959: E/DatabaseUtils(291):   at android.os.Binder.execTransact(Binder.java:320)
02-24 17:40:27.959: E/DatabaseUtils(291):   at dalvik.system.NativeStart.run(Native Method)
02-24 17:40:27.969: E/MediaStore(566): Failed to insert image
02-24 17:40:27.969: E/MediaStore(566): java.lang.UnsupportedOperationException: Unknown URI: content://media/external/images/media
02-24 17:40:27.969: E/MediaStore(566):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:146)
02-24 17:40:27.969: E/MediaStore(566):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
02-24 17:40:27.969: E/MediaStore(566):  at android.content.ContentProviderProxy.insert(ContentProviderNative.java:408)
02-24 17:40:27.969: E/MediaStore(566):  at android.content.ContentResolver.insert(ContentResolver.java:604)
02-24 17:40:27.969: E/MediaStore(566):  at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:624)
02-24 17:40:27.969: E/MediaStore(566):  at com.example.imageeditor.SecondActivity.getImageUri(SecondActivity.java:177)
02-24 17:40:27.969: E/MediaStore(566):  at com.example.imageeditor.SecondActivity.onOptionsItemSelected(SecondActivity.java:166)
02-24 17:40:27.969: E/MediaStore(566):  at android.app.Activity.onMenuItemSelected(Activity.java:2205)
02-24 17:40:27.969: E/MediaStore(566):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:748)
02-24 17:40:27.969: E/MediaStore(566):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
02-24 17:40:27.969: E/MediaStore(566):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
02-24 17:40:27.969: E/MediaStore(566):  at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
02-24 17:40:27.969: E/MediaStore(566):  at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
02-24 17:40:27.969: E/MediaStore(566):  at android.view.View$PerformClick.run(View.java:9080)
02-24 17:40:27.969: E/MediaStore(566):  at android.os.Handler.handleCallback(Handler.java:587)
02-24 17:40:27.969: E/MediaStore(566):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 17:40:27.969: E/MediaStore(566):  at android.os.Looper.loop(Looper.java:123)
02-24 17:40:27.969: E/MediaStore(566):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-24 17:40:27.969: E/MediaStore(566):  at java.lang.reflect.Method.invokeNative(Native Method)
02-24 17:40:27.969: E/MediaStore(566):  at java.lang.reflect.Method.invoke(Method.java:507)
02-24 17:40:27.969: E/MediaStore(566):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-24 17:40:27.969: E/MediaStore(566):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-24 17:40:27.969: E/MediaStore(566):  at dalvik.system.NativeStart.main(Native Method)
02-24 17:40:27.969: D/AndroidRuntime(566): Shutting down VM
02-24 17:40:27.969: W/dalvikvm(566): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-24 17:40:28.069: E/AndroidRuntime(566): FATAL EXCEPTION: main
02-24 17:40:28.069: E/AndroidRuntime(566): java.lang.NullPointerException: uriString
02-24 17:40:28.069: E/AndroidRuntime(566):  at android.net.Uri$StringUri.<init>(Uri.java:420)
02-24 17:40:28.069: E/AndroidRuntime(566):  at android.net.Uri$StringUri.<init>(Uri.java:410)
02-24 17:40:28.069: E/AndroidRuntime(566):  at android.net.Uri.parse(Uri.java:382)
02-24 17:40:28.069: E/AndroidRuntime(566):  at com.example.imageeditor.SecondActivity.getImageUri(SecondActivity.java:178)
02-24 17:40:28.069: E/AndroidRuntime(566):  at com.example.imageeditor.SecondActivity.onOptionsItemSelected(SecondActivity.java:166)
02-24 17:40:28.069: E/AndroidRuntime(566):  at android.app.Activity.onMenuItemSelected(Activity.java:2205)
02-24 17:40:28.069: E/AndroidRuntime(566):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:748)
02-24 17:40:28.069: E/AndroidRuntime(566):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
02-24 17:40:28.069: E/AndroidRuntime(566):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
02-24 17:40:28.069: E/AndroidRuntime(566):  at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
02-24 17:40:28.069: E/AndroidRuntime(566):  at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
02-24 17:40:28.069: E/AndroidRuntime(566):  at android.view.View$PerformClick.run(View.java:9080)
02-24 17:40:28.069: E/AndroidRuntime(566):  at android.os.Handler.handleCallback(Handler.java:587)
02-24 17:40:28.069: E/AndroidRuntime(566):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 17:40:28.069: E/AndroidRuntime(566):  at android.os.Looper.loop(Looper.java:123)
02-24 17:40:28.069: E/AndroidRuntime(566):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-24 17:40:28.069: E/AndroidRuntime(566):  at java.lang.reflect.Method.invokeNative(Native Method)
02-24 17:40:28.069: E/AndroidRuntime(566):  at java.lang.reflect.Method.invoke(Method.java:507)
02-24 17:40:28.069: E/AndroidRuntime(566):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-24 17:40:28.069: E/AndroidRuntime(566):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-24 17:40:28.069: E/AndroidRuntime(566):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

你添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

到清单?

我的代码没有错误。