Reputation: 1194
I want to save a file to my internal storage privately so that my application is the only one that can access it.According to the android developer site, by default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). However when I save my files using Environment.getExternalStorageDirectory(); the file is clearly accessible to anyone. I have tried using getFilesDir() instead as apparently this makes your files private but my application keeps crashing. Below is my code:
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();
}
}
}
Any suggestions? Below is my log cat:
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]
And on my screen it says Error: The document path is not valid
Upvotes: 1
Views: 5829
Reputation: 4595
Well if you want to save on the internal storage you have not to use Environment.getExternalStorageDirectory()
but you should use getFilesDir()
(Returns a File representing an internal directory for your app).
See official Android docs on the subject they are quite clear.
Upvotes: 1