I have a file on my sdcard. When I normally open and change it with “officesuite”, I can save changes. When I open it in my app as follow, “officesuite” displays it but it cannot save changes and in no way does not back to my activity. Is there any idea?
Here is my code:
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);
}
}
I check value of “Uri.parse(file.toURI().toString())” and “Uri.fromFile()” for a file in sdcard: “Uri.parse(file.toURI().toString())” starts like “file:/mnt/ ...” and “Uri.fromFile()” starts like “file:///mnt/ ...” !So I change code and it works fine:
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);
}
}