当 - java.io.FileNotFoundException:没有内容提供者时该怎么办?
Posted
技术标签:
【中文标题】当 - java.io.FileNotFoundException:没有内容提供者时该怎么办?【英文标题】:What to do when - java.io.FileNotFoundException: No content provider? 【发布时间】:2011-03-18 08:19:25 【问题描述】:当我尝试将文件附加到电子邮件时,我收到 java.io.FileNotFoundException: No content provider logcat 输出。 如果有人能告诉我我做错了什么或者我应该做什么,那就太好了。谢谢。
这就是我将文件添加到电子邮件中的方式..:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("video/3gp");
sendIntent.setType("video/mp4");
startActivity(sendIntent);
...这是整个 logcat 错误输出:
07-20 09:15:58.364: ERROR/Mms/media(168): IOException caught while opening or reading stream
07-20 09:15:58.364: ERROR/Mms/media(168): java.io.FileNotFoundException: No content provider: /sdcard/Video0006.mp4
07-20 09:15:58.364: ERROR/Mms/media(168): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:507)
07-20 09:15:58.364: ERROR/Mms/media(168): at android.content.ContentResolver.openInputStream(ContentResolver.java:345)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.model.MediaModel.initMediaSize(MediaModel.java:268)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.model.MediaModel.<init>(MediaModel.java:76)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.model.RegionMediaModel.<init>(RegionMediaModel.java:40)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.model.VideoModel.<init>(VideoModel.java:55)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.model.VideoModel.<init>(VideoModel.java:48)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.data.WorkingMessage.changeMedia(WorkingMessage.java:434)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.data.WorkingMessage.setAttachment(WorkingMessage.java:343)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.ui.ComposeMessageActivity.addVideo(ComposeMessageActivity.java:2472)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.ui.ComposeMessageActivity.addAttachment(ComposeMessageActivity.java:2544)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.ui.ComposeMessageActivity.handleSendIntent(ComposeMessageActivity.java:2521)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.ui.ComposeMessageActivity.initialize(ComposeMessageActivity.java:1703)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.mms.ui.ComposeMessageActivity.onCreate(ComposeMessageActivity.java:1633)
07-20 09:15:58.364: ERROR/Mms/media(168): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-20 09:15:58.364: ERROR/Mms/media(168): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
07-20 09:15:58.364: ERROR/Mms/media(168): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-20 09:15:58.364: ERROR/Mms/media(168): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-20 09:15:58.364: ERROR/Mms/media(168): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-20 09:15:58.364: ERROR/Mms/media(168): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 09:15:58.364: ERROR/Mms/media(168): at android.os.Looper.loop(Looper.java:123)
07-20 09:15:58.364: ERROR/Mms/media(168): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-20 09:15:58.364: ERROR/Mms/media(168): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 09:15:58.364: ERROR/Mms/media(168): at java.lang.reflect.Method.invoke(Method.java:521)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-20 09:15:58.364: ERROR/Mms/media(168): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-20 09:15:58.364: ERROR/Mms/media(168): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
【参考方案1】:也许您应该传递file:///sdcard/Video0006.mp4
形式的uri。
【讨论】:
问题是当我尝试使用 sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(uri)));它告诉我将 uri 更改为 URI 而不是字符串,当我这样做时,它会告诉我将 Uri 更改为字符串...我不知道该怎么办。 没有sdcard怎么办? @kadik 那么文件将不会存储在 SD 卡上,但 URI 仍将是相同的格式...file://[partition]/path/to/file
【参考方案2】:
你也可以试试这个:
Uri.fromFile(new File(your image path));
【讨论】:
你能看到这段代码有什么问题吗[post]。我被困在这里。请指导我。 作为我们获取和保存使用 API 24 更改的文件的方式,这是从使用 FileManagers 获取的文件中获取 URI 的最简单方法。【参考方案3】:这对我有用
val uri:Uri = File(path).toUri()
【讨论】:
【参考方案4】:将位图保存到文件的方法
private fun bitmapToFile(bitmap:Bitmap): Uri
// Get the context wrapper
val wrapper = ContextWrapper(applicationContext)
// Initialize a new file instance to save bitmap object
var file = wrapper.getDir("Images",Context.MODE_PRIVATE)
file = File(file,"$UUID.randomUUID().jpg")
try
// Compress the bitmap and save in jpg format
val stream:OutputStream = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream)
stream.flush()
stream.close()
catch (e:IOException)
e.printStackTrace()
// Return the saved bitmap uri
return Uri.parse(file.absolutePath)
【讨论】:
以上是关于当 - java.io.FileNotFoundException:没有内容提供者时该怎么办?的主要内容,如果未能解决你的问题,请参考以下文章