今天我的 Note 2 和一位拥有 SG3 的客户也犯了这个错误。我做了一些挖掘,发现了以下内容。
如果我请求外部存储设备名称 - Environment.getExternalStorageDirectory().getName()(我使用它,因为我的目标是版本 7) - 我得到'sdcard0',如果我添加我的文件名并将其传递给电子邮件或 gmail那么任何人都不会添加附件。如果我手动编辑“sdcard0”末尾的零并传递“sdcard”+我的文件名,那么一切正常。在 JB 之前,这似乎都不是问题,或者如果我在始终返回“sdcard”的模拟器中的 JB 下运行此代码。
我不确定是否应该使用不同的代码,或者这是否是操作系统报告路径和电子邮件系统使用它的方式不一致。(我原以为 sdcard0 应该指向与 sdcard 相同的位置 - 但有些东西不起作用)
我可能只是修补我的代码以去除零(我知道这是丑陋和危险的)。除非其他人有任何好主意:)
编辑:我在下面添加了我的代码,以防其他人发现它有用。
// try and grab the log file
String logFile = File.separator + getResources().getString(R.string.app_name) + File.separator + "logs" + File.separator + Constants.SMS_FILE;
String extStorage = Environment.getExternalStorageDirectory().getName();
// The following is a kludge for the Samsung (and maybe more) JB devices that don't allow you to add a file with the SDCARD0 external storage
// link
File nf = new File(extStorage, logFile);
if (!nf.exists()) {
// OK we can't find the file - say so and see if we are hiding behind SDCARD0
Log.w(getLocalClassName() + ".shouldOverrideUrlLoading", "File: " + extStorage + logFile + " Doesn't exist - trying again");
if (extStorage.matches("(?i)SDCARD(?-i)[0-9]")) {
// OK we've got a SDCARDx scenario - let's see if the file exists without the x
String extStorageFix = extStorage.replaceFirst("(?i)SDCARD(?-i)[0-9]", extStorage.substring(0, 6)); // try it without the 0,1,2 etc
nf = new File(extStorageFix, logFile);
if (!nf.exists()) {
// OK we're in the pooh now - can't find the log file so just say so and try to exit without attaching it.
Log.w(getLocalClassName() + ".shouldOverrideUrlLoading", "File: " + extStorageFix + logFile + " Doesn't exist either - giving up");
Toast.makeText(
getApplicationContext(),
"Unable to attach Log file from\n" + extStorage + logFile + "\n" + extStorageFix + logFile
+ "\nPlease add it manually if possible", Toast.LENGTH_LONG).show();
} else {
// Hurrah we found it - remember we did so and carry on
extStorage = extStorageFix;
Log.w(getLocalClassName() + ".shouldOverrideUrlLoading", "Found logfile at: " + extStorage + logFile);
}
}
}
if (Debug.ON) {
Log.d(getLocalClassName() + ".shouldOverrideUrlLoading", "Filepath = " + "file://" + File.separator + extStorage + logFile);
}
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + File.separator + extStorage + logFile));**strong text**