我无法将图像附加到外部存储的邮件中。 我试着用这一行。
Uri image = Uri.parse("/storage/emulated/0/Pictures/park.jpg");
在这种情况下,mail
会显示can't attach empty file
等消息。
之后我尝试执行以下代码。它显示了一些崩溃报告。我已经完成了一些先前提出的问题。我仍然无法找到完美的答案。
以下是我正在使用的代码......
Uri image = FileProvider.getUriForFile(this, "com.example.chint.asyntaskexample", new File("/storage/emulated/0/Pictures/", "park.jpg"));
//Context
//Package
//File path (/storage/emulated/0/Pictures/park.jpg)
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_STREAM, image);
i.setType("image*//*");
Intent chooser = Intent.createChooser(i, "Send Image");
if (i.resolveActivity(getPackageManager()) != null)
startActivity(chooser);
else
Toast.makeText(this, "Nothing", Toast.LENGTH_SHORT).show();
点击crash
按钮后,我收到了share
。
这是logcat
...
com.example.chint.asyntaskexample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chint.asyntaskexample, PID: 7157
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
at com.example.chint.asyntaskexample.MainActivity.sendImage(MainActivity.java:105)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
请帮帮我..
提前致谢。
答案 0 :(得分:0)
Uri image = Uri.parse("/storage/emulated/0/Pictures/park.jpg");
这不是有效的Uri
,因为它没有方案。此外,不要硬编码路径。除此之外,这不适用于Android 7.0+,因为已禁止传递file
Uri
值。
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
显然,您的<provider>
没有FileProvider
元素,其权限字符串为com.example.chint.asyntaskexample
。
此外,image*//*
不是有效的MIME类型。
答案 1 :(得分:0)
以下是我想出的答案......
{{1}}