我试图在单击按钮时发送电子邮件,并将附件存储在内部存储器中。我尝试按照本教程实施文件提供程序,但是每次我尝试运行该应用程序并按下按钮时,该应用程序都会崩溃并停止工作。
我迷失了为什么会这样。下面是相关代码:
清单:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false"
android:authorities="${applicationId}">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"/>
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<cache-path name="cache" path="/" />
<files-path name= "files" path="/" />
</paths>
</PreferenceScreen>
MainActivity.java
Button emailBtn = (Button) findViewById(R.id.button);
main = findViewById(R.id.main);//email form button
emailBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap b = Screenshot.takescreenshotOfRootView(main);
ImageView programLogo = (ImageView) findViewById(R.id.imageView);
programLogo.setImageBitmap(b);
File filePath = new File(loadImageFromStorage(saveToInternalStorage(b)), "profile.jpg");
Intent intent = new Intent(Intent.ACTION_SEND, Uri.fromParts(
"mailto","myemail@gmail.com", null));
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uri = (Uri) FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID, filePath);
intent.setDataAndType(uri, "application/jpg");
String subject = "please work";
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Choose an Email client :"));
}
});
}
private String saveToInternalStorage(Bitmap bitmapImage) {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File mypath = new File(directory, "profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return directory.getAbsolutePath();
}
private String loadImageFromStorage(String path) {
File f = new File(path, "profile.jpg");
return f.getAbsolutePath();
}
这是我得到的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication234567, PID: 5272
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.myapplication234567/app_imageDir/profile.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:418)
at com.example.myapplication234567.MainActivity$1.onClick(MainActivity.java:64)
at android.view.View.performClick(View.java:6913)
at android.view.View.performClickInternal(View.java:6890)
at android.view.View.access$3200(View.java:792)
at android.view.View$PerformClick.run(View.java:27158)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:209)
at android.app.ActivityThread.main(ActivityThread.java:7021)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:486)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:872)
答案 0 :(得分:-1)
您的消息包含此路径,该路径显示不正确:/data/data/com.example.myapplication234567/app_imageDir/profile.jpg/profile.jpg
尝试捕获是您的朋友。