Save a file on Android documents? - android

I'm creating an excel file directly on Android. The code is working fine, I can verify the .xls has been created and I can check it's content from a rooted phone.
However I want save the excel file in a different folder from the project itself (which is /data/data/mypackage/files/). I want to save it on the normal phone documents, which would be something like "/storage/sdcard0/Documents/test.xls". I get a message saying "Permission denied" when trying to save the .xls file on that folder.
How can I get access to those folders?
Here I'm creating the excel file
WriteExcel test = new WriteExcel();
test.setOutputFile("/data/data/com.example.interfaz/files/test.xls"); // here is where I want to save the file to "/storage/sdcard0/Documents/test.xls"
try {
test.write();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}

Did you add permissions to your manifest?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You should also not hard code the SDCard location since it can, and does, changes between devices.
Instead, use Environment.getExternalStorageDirectory()

Check out this page for more info on saving files:
http://developer.android.com/training/basics/data-storage/files.html#GetWritePermission
Make sure you have the permission in your manifest to write files to external storage:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>

Related

share app screenshot without WRITE_EXTERNAL_STORAGE permission?

Is it possible to share own app screenshots without adding any permissions ?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
And this is my others apps code which is works on copy to cache and share
public void sendFile(File paramFile) {
String fileExtension = ".apk";
File temporaryFile;
try {
temporaryFile = File.createTempFile(paramFile.getName(), fileExtension, getApplicationContext().getExternalCacheDir());
copy(paramFile, temporaryFile);
} catch (Exception e) {
temporaryFile = paramFile;
}
Intent localIntent = new Intent();
localIntent.setAction("android.intent.action.SEND");
localIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(temporaryFile));
localIntent.setType("application/*");
localIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(localIntent, ""));
}
Save your image to internal storage (e.g., getCacheDir()), then use FileProvider to make it available to other apps. See the documentation for more.
You are using getExternalCacheDir()
it absolute the path to youraplication specific directory on the primary
external storage device where the application can place cache
files it owns. These files are internal to the application, and not
typically visible to the user as media.
There is no security enforced with these files.
no need to add the permissions to read or write to the returned path. it's always accessible to the youraplication app. This only applies to paths generated for package name of the youraplication.
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
android.Manifest.permission.READ_EXTERNAL_STORAGE

Unable to read shape file in Android

I am trying to use ShapefileFeatureTable() to read shape file in android. No matter what path I give, it says, "File Not Found" exception.
try
{
ShapefileFeatureTable shpFileFeatTable = new ShapefileFeatureTable("/storage/sdcard/map.shp");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
Can anyone help me?
Note: I am working on Android Emulator. Also I am using arcGIS library.
see this example if helps you
https://developers.arcgis.com/android/api-reference/reference/com/esri/core/geodatabase/ShapefileFeatureTable.html
Your app is probably not requesting permission to read from external storage. You need to put the following in AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
At the moment, a ShapefileFeatureTable is read-only, but if this class gets editing capabilities in the future and you want to do editing, your app will need to request write permissions as well:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
See http://developer.android.com/guide/topics/manifest/uses-permission-element.html for more details.
It's also possible (but not likely) that your file path is simply incorrect. If you request read permissions and still get the FileNotFoundException, check the value of new File("/path/to/file.shp").exists(). If Android can't see the file, ArcGIS can't see the file either.
It turns out, it was displaying the shapefile. I had to blank the MapView to see it.
<com.esri.android.map.MapView
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</com.esri.android.map.MapView>
I had same problem. Be sure that other files related to map.shp there are beside of it on same folder. Extensions of other files related to map.shp are: .dbf .prj .sbn .sbx .shp.xml .shx

Trying to save a file on an android system and make it downloadable by the user

I am sorry I am extremely new to android and I am lost. I successfully found out how to save a file on an android system, but every time I try to search for the file on my Android phone (which is where I have a copy of this app located) I am unable to locate it. I know it is there because the app would not start up with out it. How do you write a file that can be both used by the App AND searched by the user in the File Explorer. I am using a Galaxy Note 3 Android version 5.0 Any help would be appreciated.
private void WriteFile(String FileName, String FileCont, boolean isAppend){
File file = new File(getApplicationContext().getFilesDir().getAbsolutePath() + "/" + FileName);
try {
FileOutputStream stream = new FileOutputStream(file, isAppend);
stream.write(FileCont.getBytes());
stream.close();
}
catch(Exception ex){
}
}
Did set the permissions to write on external space in your manifest.xml?
For reference see
http://developer.android.com/reference/android/Manifest.permission.html
You have to set the "WRITE_EXTERNAL_STORAGE" permission to write on external disk. Just add the following line to your android manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
It will implicitly contain the permission to read external storage as well ;).

mkdirs seems to not work on sdcard in onCreate of an Activity

I am using a method to create a directory structure using mkdirs in onCreate of an activity. Eventhough it returns true and all indiactions are that it should work, still the directories are not created (or perhaps not visible) on the sdcard. What might be the problem?
Update:
1. I have given the android.permission.WRITE_EXTERNAL_STORAGE
2. The method works fine if called from anywhere else other than onCreate, i.e the directory structure is created.
public static void createNoMediaFile() {
Log.v("myreader",">>>>>>>>>>>>>>> Entered createNoMediaFile");
File papermag=new File(DigitalEditionConstant.PAPERMAG_PATH);
boolean isdircreated=papermag.mkdirs();
Log.v("myreader",">>>>>>>>>>>>>>>>Directory setup: "+isdircreated);
File noMediaFile=new File(DigitalEditionConstant.DIR_PATH+".nomedia");
if(!noMediaFile.exists()){
try {
noMediaFile.createNewFile();
Log.v("myreader",">>>>>>>>>>>>>>>>>>> Created File: "+noMediaFile.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Directory paths etc. are all checked and ok. This method is called from onCreate of an activity in my project
have you added permission of WRITE_EXTERNAL_STORAGE in your manifest file? see here
here is simple code
File wallpaperDirectory = new File(Environment.getExternalStorageDirectory().toString()+"/FolderName");
wallpaperDirectory.mkdirs();
add this permission in your menifest...
permission of WRITE_EXTERNAL_STORAGE
does the name of the dir start with a period? DDMS and most file manager apps wont show those dirs..but they are there.
Have you added the manifest permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Having the device mounted as USB will block access to the SD from code also.
Use "adb push" command to check if you are able to copy the directory to the SD card (to determine that there is no issue with the SD card)

It can't access the sdcard after setting "android.uid.system" in my application?

This is a strange problem. My Application can access the sdcard successfully if I don't set android.uid.system to it. But after setting android.uid.system to it, my application can't access the sdcard. At this time, the exception take place:07-13 09:11:24.999: INFO/System.out(9986): create file happen exception--->java.io.IOException: Permission denied. I check I write the right permission in the right place:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />.
Because use forceStopPackage in my application, I need to add android.uid.system to the manifest. And I have written LOCAL_CERTIFICATE := platform in the make file. Who can explain this strange problem. After setting android.uid.system, my application is belong the system process which should have more power to access the sdcard. This is my idea. The following are my code:
public void setPackage(String dir){
System.out.println( "setPackage dir=="+dir );
File share=new File("/mnt/sdcard","test.txt");
if(!share.exists()){
try{
share.createNewFile();
}catch(Exception e){
System.out.println( "creat file happen exception--->" +e.toString() );
}
}
try{
if(share!=null){
System.out.println( "create file is not null" );
FileOutputStream fops=new FileOutputStream(share);
fops.write(dir.getBytes());
fops.flush();
fops.close();
}
}catch(Exception e){
System.out.println( "write Exception-->" +e.toString() );
}
}
And My application run at the emulator and his target version is 2.3. Thank you very much.
Please read this: link1
and this link2
Use Environment.getExternalStorageDirectory().getAbsolutePath() to get the path, NOT "/mnt/sdcard"
Use: File share = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"test.txt");
This won't work: File share = new File("/mnt/sdcard", "test.txt");
You can see in android source code: frameworks\base\core\java\android\os\Environment.java, it have a function:
private static void throwIfSystem() {
if (Process.myUid() == Process.SYSTEM_UID) {
Log.wtf(TAG, "Static storage paths aren't available from AID_SYSTEM", new Throwable());
}
}
This function would be called getExternalStorageDirectory(), hence app with system uid can't access sdcard if you don't hack aosp.
android.uid.system make your app bind system, but sdcard observered by system also, then if your app delete this, it can access the sdcard. it seems
delete this line in your xml file :android:sharedUserId="android.uid.system"ystem

Categories

Resources