因此,我已经尝试了一天,在SD卡上创建.txt文件,只是发现不是我的代码错了,但是KitKat不再允许开发人员在外部SD卡上存储文件;周围是否有诸如临时.txt文件之类的东西,或者有一些创建文件然后发送的新代码?这是我当前的代码不起作用
String DataIn = PhoneNumber + "," + dataLong + "," + dataLat;
try
{
File storageDirectory = new File ("/sdcard/LocationData.txt");
storageDirectory.createNewFile();
FileOutputStream fout = new FileOutputStream(storageDirectory);
OutputStreamWriter myoutwriter = new OutputStreamWriter(fout);
myoutwriter.append(DataIn);
myoutwriter.close();Toast.makeText(getBaseContext(),"Saved", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
Intent emailintent = new Intent(Intent.ACTION_SEND);
emailintent.setType("text/plain");
emailintent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
emailintent.putExtra(Intent.EXTRA_SUBJECT, "Data");
emailintent.putExtra(Intent.EXTRA_TEXT, "Hello World!");
File root = Environment.getRootDirectory();
String DataAttachment = "/sdcard/LocationData.txt";
File filer = new File(root, DataAttachment);
if (!filer.exists() || filer.canRead())
{
return;
}
Uri uri = Uri.fromFile(filer);
emailintent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailintent, "Choose an Email provider"));
File dirs[] = ContextCompat.getExternalFilesDirs(getApplicationContext(),null);
String Info = "getExternalFilesDirs()\ndirs.length: " + dirs.length;
int nr = -1;
while ( ++nr < dirs.length )
Info += "\n"+ dirs[nr].getAbsolutePath();
Toast.makeText(this, Info, Toast.LENGTH_LONG).show();
您需要/Project/libs/android-support-v4.jar文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句