我想附加一个位于 data/data/com.example.XXXX/files/THISFILE.txt 中的文件
但首先我应该将它导出到 SD 以便附加它,我在下面有这段代码但没有创建“hello.txt”文件。所以我不能附加它。
public class exportar extends Activity {
String from = "/data/data/com.example.XXXXXX/files/THISFILE.txt";
String to = "/sdcard/hello.txt";
public static boolean copyFile(String from, String to) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(from);
if (oldfile.exists()) {
InputStream inStream = new FileInputStream(from);
FileOutputStream fs = new FileOutputStream(to);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
return true;
} catch (Exception e) {
return false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_SUBJECT,"XXX");
i.putExtra(Intent.EXTRA_TEXT , ("XXX"));
String file = "file://" + Environment.getExternalStorageDirectory()+"/hello.txt";
Uri uri = Uri.parse(file);
i.putExtra(Intent.EXTRA_STREAM, uri);
finish();
try {
startActivity(Intent.createChooser(i, "xxxx."));
} catch (android.content.ActivityNotFoundException ex) {
finish();
}
}
}
任何建议将不胜感激。
已编辑 1
我改变了
String from = "/data/data/com.example.XXXXXX/files/THISFILE.txt";
String to = "/sdcard/hello.txt";
至
File ruta_data = Environment.getDataDirectory();
File from = new File(ruta_data.getAbsolutePath(), "com.example.aa/files/THISFILE.txt");
File ruta_sd = Environment.getExternalStorageDirectory();
File to = new File(ruta_sd.getAbsolutePath(), "Hello.txt");
我放 getDataDirectory()
是因为它在 data/data/ 但我不知道如果它是 n data/data/com.example.aa/files/THISFILE.txt 我该怎么办
如果我想将它复制到 SD 的根目录,ruta_sd 是否正确?