My app downloads a file to my Android tablet. I can see the file on my tablet, but I can't find the file using my computer's file manager.
A response to another Stack Overflow question says the file manager may need "root privileges," but I don't know how to give a file manager root privileges or download a file manager that has root privileges. Is that possible? A second answer to that same question mentions some way to transfer the file using the command line, but I haven't been able to figure out how to do that.
I need to be able to transfer the file to my computer. What is the easiest way to do this?
void newFile() {
try {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "file.txt");
FileOutputStream fos = new FileOutputStream(file);
String myInputText = "I really hope this works!";
fos.write(myInputText.getBytes());
fos.close();
System.out.println("My file is at " + file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
If you familiar with Linux Operating system, that you can use command line to chmod the file privilege. But I think if you can move this file in your tablet, maybe try to transfer it by Blue tooth, email is much easier.
Related
I need to use a file in my application. If i upload the file to Data/Data/APP/files then it is added with -rw-rw-rw permissions which i can then use in my application. If i programatically write the file to getFilesDir() the exact same directory, i can see the 2 exact files in the same directory, however the programatically saved file has permissions -rw------- i cannot then access the file in my app using getfilesDir().
this is how the file is saved:
public void writeFileOnInternalStorage(Context mcoContext,String sFileName, String sBody){
File file = new File(getApplicationContext().getFilesDir(), "");
if(!file.exists()){
file.mkdir();
}
try{
File gpxfile = new File(file, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
}catch (Exception e){
e.printStackTrace();
}
}
How can i get the correct permissions to use the file. It may well not be a permissions issue it maybe the way i am saving the file? It is a .graphml extension file.
What do you mean by cannot access your file?
Take a look at this documentation (https://developer.android.com/training/data-storage), you don't need permission to access (read and write) files inside App-Specific Directory.
Edit:
I'm assuming that you already stored the file to the disk.
Please note that to read and write files inside App-Specific Directory doesn't require permission. You can read it using this simple code.
public File readFile(Context context, String fileName) {
File file = new File(context.getFilesDir(), fileName);
// do other stuff, like checking if the file exist, etc.
return file;
}
It doesn't matter what file extension it is, as long as the file exists, you will get it.
Actually there are so many articles that already cover this topic, please take a look to understand this topic better.
I am asking of there's a way how I could put like a program or a bat file or any file that has stuff written in it and them when he clicks on a button it will create that file that i have put into my project on to the users desktop is there a way?
File test = new File("C:/Users/"
+ System.getProperty("user.name")
+ "/AppData/Roaming/.minecraft/mods/welcome.txt");
try { test.createNewFile(); }
catch (IOException e1) { e1.printStackTrace(); }
this doesnt work.
If the file you want to creating is already exist in the disk, then you can print a message like the "File already exists" -
try {
File file = new File("c:\\some\location\file_name.txt");
if (file.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File already exists.");
}
}catch (IOException e) {
e.printStackTrace();
}
In order to just put a file on an other computer you will need to be granted permission in some way to do that (otherwise anyone could just infect any networked computer with any content they desired). There are several ways you could gain access to an other computer (look into virtual private network and mapped network drive). The most common way of delivering files to another computer is to set up a web site where the user can request the file to be downloaded to their machine by clicking on a link. You could also write a client program that used an http get to allow the client user to request content be downloaded to a specific place on their machine.
ok i found out how i just downloaded the files from a server that i have "made"
I am trying to get the following code to work properly. It always prints the output from the catch block, even though the output that is only printed if the file exists, is printed.
String outputFile = "/home/picImg.jpg";
File outFile = new File(outputFile);
if(outFile.exists)
newStatus(" File does indeed exist");
FileOutputStream fos;
try {
fos = new FileOutputStream(outFile);
fos.write(response);
fos.close();
return outputFile;
} catch (FileNotFoundException ex) {
newStatus("Error: Couldn't find local picture!");
return null;
}
In the code response is a byte[] containig a .jpg image from a URL. Overall I am trying to download an image from a URL and save it to the local file system and return the path. I think the issue has to do with read/write permissions within /home/. I chose to write the file there because I'm lazy and didn't want to find the username to find the path /home/USER/Documents. I think I need to do this now.
I notice in the terminal I can do cd ~ and get to /home/USER/. Is there a "path shortcut" I can use within the file name so that I can read/write in a folder that has those permissions?
No. The ~ is expanded by the shell. In Java File.exists() is a method, you can use File.separatorChar and you can get a user's home folder with System property "user.home" like
String outputFile = System.getProperty("user.home") + File.separatorChar
+ "picImg.jpg";
File outFile = new File(outputFile);
if (outFile.exists())
Edit
Also, as #StephenP notes below, you might also use File(File parent, String child) to construct the File
File outFile = new File(System.getProperty("user.home"), "picImg.jpg");
if (outFile.exists())
~ expansion is a function of your shell and means nothing special for the file system. Look for Java System Properties "user.home"
Java provides a System property to get the user home directory: System.getProperty("user.home");.
The advantage of this is, that it works for every operating system that can run the Java virtual machine.
More about System properties: Link.
I am trying to read a pdf file from internal memory of the device my code is here:
File pdfFile;
pdfFile=new File("data/data/com.myapp.main/app_c"+md+"/c"+md+".pdf");
if(pdfFile.exists())
{
try{
FileOutputStream fileOutput = openFileOutput(pdfFile.toString(), Context.MODE_WORLD_READABLE);
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(ChTable.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
But the Pdf reader shows an Error -File not supported-Or File Not found .But I have checked that file is there at this location.I have also changed the permission to the file ,but still the same result. Would Someone help me detect and solve my problem ?
you can use PDFBox library to read data from pdf -
http://pdfbox.apache.org/
data/data/com.myapp.main/app_c"+md+"/c"+md+".pdf"
Is this path correct?
I am guessing the path should be like below.
data/data/com/myapp/main/app_c"+md+"/c"+md+".pdf"
You should be using one of the apis to get the application's files directory rather than assuming what the path will be.
Your actual problem however is most likely that any file you create there is probably private to the owning application, so the PDF reader app lacks permission to access it.
Solutions to that would be to change the file mode to world readable, or more commonly to put the file on the external storage (ie, "sdcard") rather than the internal memory, as that does not (to date) have a concept of permissions. Though it's worth remembering that a device isn't guaranteed to have an external storage, or for it to be accessible at any given point in time even if it exists.
I am trying to open a file i just created in my code (so i am sure that the file exists)
The code is like this:
File file = new File(filename);
file.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
...
bw.close();
try {
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
desktop.open(file);
} catch (Exception e) {
...
}
But as the title says i get a "java.io.IOException: The system cannot find the path specified" from the desktop.open(file) istruction.
The problem surely is that the file pathname contains spaces (which are translated into "%20"). Is there a way to avoid this?
I found the real problem.
It wasn't either the %20 as i supposed.
I just hadn't the privileges to directly access the file location. It's a bit complicated to explain...
i'm just sorry i coulnd't figure out the real problem before.
Thanks for your suggestions anyway!
Are you using an IDE? What is inside the variable 'filename' (it's actual contents). Line two is unnecessary.
Is the error from the stack trace pointing to BufferedWriter bw = new BufferedWriter(new FileWriter(file)); or desktop.open(file);
EDIT:
You can also try the following code
File myCSVFile; //reference to your csv file here
String execString = "excel " + myCSVFile.getAbsolutePath();
Runtime run = Runtime.getRuntime();
try {
Process pp = run.exec(execString);
} catch(Exception e) {
e.printStackTrace();
}
The java.io error is appearing because it's failing to open the file. The code above will force excel open with your file as the argument. You'll need to set your environment variable to ensure that the command 'excel' in the command line opens the Excel application.
If you're planning on releasing this application for use you can ensure that excel is installed by checking the registry, then checking the install location of Excel from there.
Try to open a different file with other applications and see if other file types are supported. As Clarisse said, IOException is thrown from the 'open' method if the specified file has no associated application or the associated application fails to be launched. If the specified file doesn't exists IllegalArgumentException is thrown, which is not in your case. If for some reason opening a CSV file with Desktop doesn't work for you, try using krslynx approach. Same can be found here. You can quickly assemble a test application for opening anything on your machine using the code found here
In the Desktop javadoc it's written :
IOException - if the specified file has no associated application or the associated application fails to be launched
So are you sure your filetype has a default application associated ?
As krslynx says, file.createNewFile() is unnecessary. However file.mkdirs() may be necessary instead, if the intermediate directories don't exist yet.
EDIT: it's not clear from your question whether this is happening in new FileWriter() or in Desktop.open(). Please clarify.