我的SD卡上有一个文件。当我通常打开并使用“officesuite”进行更改时,我可以保存更改。当我在我的应用程序中打开它时,“officesuite”显示它,但它无法保存更改,绝不会回到我的活动。有什么想法吗? 这是我的代码:
import java.io.File;
import java.net.URI;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.webkit.MimeTypeMap;
public class OfficesuiteSaveAsActivity extends Activity {
private String fileDir;
private File file;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedlnstanceState) {
super.onCreate(savedlnstanceState);
setContentView(R.layout.main);
String sdCardDir = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String fileName = "TestofficeSuite.docx";
fileDir = sdCardDir + File.separator + fileName;
file = new File(fileDir);
displayFile(file.toURI());
}
public void displayFile(URI uriString) {
Uri data = Uri.parse(uriString.toString());
Intent newlntent1 = new Intent();
newlntent1.setAction(Intent.ACTION_VIEW);
MimeTypeMap mtm = MimeTypeMap.getSingleton();
newlntent1.setDataAndType(data, mtm.getMimeTypeFromExtension("docx"));
startActivity(newlntent1);
}
}
答案 0 :(得分:1)
我检查sdcard中文件的“Uri.parse(file.toURI()。toString())”和“Uri.fromFile()”的值: “Uri.parse(file.toURI()。toString())”像“file:/ mnt / ...”一样开始,“Uri.fromFile()”的开头就像“file:/// mnt / ...” !所以我改变代码并且工作正常:
public class OfficesuiteSaveAsActivity extends Activity {
private String fileDir;
private File file;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedlnstanceState) {
super.onCreate(savedlnstanceState);
setContentView(R.layout.main);
String sdCardDir = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String fileName = "TestofficeSuite.docx";
fileDir = sdCardDir + File.separator + fileName;
file = new File(fileDir);
displayFile(file);
}
public void displayFile(File file) {
Uri data = Uri.fromFile(file);
Intent newlntent1 = new Intent();
newlntent1.setAction(Intent.ACTION_VIEW);
MimeTypeMap mtm = MimeTypeMap.getSingleton();
newlntent1.setDataAndType(data, mtm.getMimeTypeFromExtension("docx"));
startActivity(newlntent1);
}
}