我想将文件私下保存到我的内部存储中,以便我的应用程序是唯一可以访问它的应用程序。根据 android 开发者网站,默认情况下,保存到内部存储中的文件对您的应用程序是私有的,其他应用程序无法访问他们(用户也不能)。但是,当我使用 Environment.getExternalStorageDirectory(); 保存文件时 任何人都可以清楚地访问该文件。我尝试使用 getFilesDir() 代替,因为显然这会使您的文件私有,但我的应用程序不断崩溃。下面是我的代码:
public class SaveOpen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save_open);
isSDCardAvailable();
Button david = (Button) findViewById(R.id.button1);
david.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File pdfFile = new File(getFilesDir().getAbsolutePath() + "/MathBarsPDFDocumentTestNew/userg.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// No application to view, ask to download one
AlertDialog.Builder builder = new AlertDialog.Builder(
SaveOpen.this);
builder.setTitle("No Application Found");
builder.setMessage("Download one from Android Market?");
builder.setPositiveButton("Yes, Please",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent.setData(Uri
.parse("market://details?id=com.adobe.reader"));
startActivity(marketIntent);
}
});
builder.setNegativeButton("No, Thanks", null);
builder.create().show();
}
}
});
}
public void isSDCardAvailable(){
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.toString().equals(state.toString())) {
//File sdDir = Environment.getExternalStorageDirectory();
File sdDir = getFilesDir();
File newdir = new File(sdDir.getAbsolutePath() + "/MathBarsPDFDocumentTestNew");
newdir.mkdirs();
File file = new File(newdir, "userg.pdf");
try {
FileOutputStream f = new FileOutputStream(file);
AssetManager assetManager = getAssets();
InputStream input = assetManager.open("userguide.pdf");
byte[] buffer = new byte[1024];
int read;
while ((read = input.read(buffer)) != -1) {
f.write(buffer, 0, read);
}
f.close();
input.close();
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
}
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(
SaveOpen.this);
builder.setTitle("No SD Card available");
builder.setMessage("Please Insert SD Card");
builder.setNegativeButton("Cancel", null);
builder.create().show();
}
}
}
有什么建议么?下面是我的日志猫:
03-05 11:02:19.520: I/ActivityManager(164): START {act=android.intent.action.VIEW dat=file:///data/data/com.david.openpdf/files/MathBarsPDFDocumentTestNew/userg.pdf typ=application/pdf flg=0x4000000 cmp=com.adobe.reader/.AdobeReader} from pid 2684
03-05 11:02:19.570: I/ActivityManager(164): START {cmp=com.adobe.reader/.ARViewer (has extras)} from pid 2697
03-05 11:02:19.620: I/ActivityManager(164): Displayed com.adobe.reader/.AdobeReader: +77ms
03-05 11:02:19.660: D/OpenGLRenderer(2684): Flushing caches (mode 0)
03-05 11:02:19.680: V/TabletStatusBar(243): setLightsOn(true)
03-05 11:02:19.700: D/OpenGLRenderer(2684): Flushing caches (mode 1)
03-05 11:02:35.630: E/System(2697): Uncaught exception thrown by finalizer
03-05 11:02:35.630: E/System(2697): java.lang.NullPointerException
03-05 11:02:35.630: E/System(2697): at com.omniture.AppMeasurementBaseSE13.finalize(AppMeasurementBaseSE13.java:43)
03-05 11:02:35.630: E/System(2697): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
03-05 11:02:35.630: E/System(2697): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
03-05 11:02:35.630: E/System(2697): at java.lang.Thread.run(Thread.java:856)
03-05 11:02:35.660: D/dalvikvm(164): GC_CONCURRENT freed 1044K, 26% free 11680K/15751K, paused 2ms+5ms
03-05 11:02:44.640: I/wpa_supplicant(229): wlan0: WPA: Group rekeying completed with 00:1c:b3:ae:95:09 [GTK=CCMP]
在我的屏幕上显示错误:文档路径无效