I'm sorry to take your time, so I'll try to make it short. Before all, I apologize if my English is wrong or if I'm making dumb mistake, English is not my mother tong. I'm sorry if the answer is silly I'm still not experimented with the making of android apps. I'm making a android application and I'm trying to store some files on a public folder on the external storage. I'm having an issue and I have no idea what might be happening.
The issue is so :
I have a file in the 'download' directory. Then I'm moving the file to my public folder, then I rename it (all this through code). Everything seems to happens fine, the file is correctly moved, is readable and writeable. The file is then visible through app like 'File commander'. Although, when I connect my phone with my computer the file is nowhere to be seen with nautilus (ubuntu folder app), even with the terminal (sudo ls -a doesn't change a thing).
String path = (Environment.getExternalStoragePublicDirectory("myAppFolder").getAbsolutePath() + "/inside_folder/" + fileToMove.getName())
File folder = new File(path);
folder.mkdir();
File thatFile = new File (folder.getAbsolutePath() + "/downloading");
if (thatFile.exist()) thatFile.delete();
try {
FileInputStream instream = new FileInputStream(fileToMove);
FileOutputStream outstream = new FileOutputStream(thatFile);
FileChannel inChannel = instream.getChannel();
FileChannel outChannel = outstream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
instream.close();
outstream.close();
outChannel.close();
inChannel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
thatFile.renameTo(new File(folder.getAbsolutePath + "/coucou"));
For some reason, if the file is rename with 'file commander', the file will now be visible through nautilus. Also, if no other file than the one invisible named 'coucou' then, I will have no way to explore to myAppFolder/inside_folder/imafile/coucou , myAppFolder (and so inside_folder) being unreachable. How can I change that ? How can I make it visible without File Commander. Also, I wonder how can a file be invisible ... anyway thank you for your time and help :-).
edit: Rebooting the device clear the issue. Yet, if new file are created, the won't appear until new reboot (or for now it seems so). I don't want the user to do so each time. Does anyone know how to fix that ? Thank you :).
Related
I'm having a little problem with my android app.
My app generates a .html file when a "export button" is pressed.
But I can't see the file in my pc or in the Android's Download app. I can only see it in Astro file manager.
That's how I generate and saved my file .
String string = "Hello World"
String filename = "/sdcard/Download/teste.html";
FileOutputStream outputStream;
try {
File file = new File(filename);
boolean newFile = file.createNewFile();
if(!newFile){ //if the file exists I delete it and generate a new file
file.delete();
newFile=file.createNewFile();
}
Context context=getActivity();
FileOutputStream fOut = new FileOutputStream(file,true);
// Write the string to the file
fOut.write(string.getBytes());
/* ensure that everything is
* really written out and close */
fOut.flush();
fOut.close();
}
catch (Exception e) {
e.printStackTrace();
}
I suppose there is a way to visualize this file without the Astro app but I can't find how do this, if someone can help I'll be grateful.
Thanks
First, never hardcode paths. Your path will be wrong on some Android devices. Please use the proper methods on Environment (e.g., getExternalStoragePublicDirectory()) or Context (e.g., getExternalFilesDir()) to get the roots under which you can safely place files.
Beyond that, files that you write to external storage will not be visible to PCs until that file is indexed by MediaScannerConnection, and even then it might require the user to perform some sort of "reload" or "refresh" operation in their file browser to see it.
I have another blog post with more about external storage which may be of use to you.
I am in need to create a text file where the input is taken from the user.
I am trying to save the txt file in a folder(folder should be created even if it is not there)
My code is as follows,
FileOutputStream fileos = null;
if (FreeMemory() != 0) {
FileOutputStream fos=null;
try {
File path=new File(getFilesDir(),"sri");
if(!path.exists()){
path.mkdir();
}
File mypath=new File(path,"myfile.txt");
if (!mypath.exists()) {
fos = new FileOutputStream( mypath);
String text="Write Hii";
fos.write(text.getBytes());
fos.close();
}
}catch(Exception e){
}
}
The "path" variable gives this path: "/data/data/com.example.gm/files/sri"
I navigated to this path in my device: Android-->Data-->com.example.gm-->files-->
but the folder "sri" is not created and even the file also. Am I navigating to th ecorrect path ? I am not able to find out the file in the device.
I searched for the folder by installing "Astro File Manager" app too. But, couldn't find it. I am not getting any Exception when writing to the folder. The code must be correct. But where is the folder and file? Please anyone help me in solving this.
I want to save the txt file in internal memory of my device.
Whats wrong with my code? Please suggest me the solution.
I have gone through many trails and finally approached stackoverflow.
Thanks for any help!!
The data in the internal storage is not accessible out side the application owning it, so any third party application like file browser or Astro File Manager will not be able to see the files. The application with root access will only be able to access the files in the internal memory. I believe you should programmatically verify if the file is created. getFilesDir() will give the internal memory location allocated to that app.
Have you give the permission in manifiest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
First of all, I want to know if making a file without extension is okay. For example, making a file with ".txt" extendsion will make it a txt file on a computer, but I don't know it matters in android. And I noticed that when working on eclipse, I could deleted a fild with the extensions but couldn't delete a file without a extension.
I want my program to delete a file in the internal storage (com.name.application folder) but it's not working for me.
I make a file with
FileOutputStream fos = openFileOutput(FileName, MODE_PRIVATE);
write with
bos = new BufferedOutpuStream(openFileOutput(FileName,Context.MODE_APPEND)
bos.write(newVocabFile.getBytes());
and I want to delete with
FILE file = new File(fileName);
fild.delete();
I did researches on google and applied different methods to my codes, but every method did not work for me. Because .delete() does not work properly, .exist() does not work either. I tired making the mile with and without extension but both ways did not work either.
I really need to get through this in order to finish my application. Please help me
You have a typo on this line
FILE file = new File(fileName);
fild.delete();
It should be
File file = new File(fileName);
file.delete();
I want to create a new xml file in android.my code is like below.It didn't work
File newxmlfile = new File("C:/Users/yunus.oksuz/Desktop/xmlFile.xml");
try
{
newxmlfile.createNewFile();
Toast msg = Toast.makeText(MainActivity.this, "file was created", Toast.LENGTH_LONG);
msg.show();
}
catch(IOException e)
{
Log.e("IOException", "exception in createNewFile() method");
}
First thing to know, you cannot even think about having a file in your PC and trying to refer it to your Android application project. They both are totally different.
let me explain the reason.
Consider you have the app installed in a phone, how do you think you can simply access your PC to which you are no more connected. This is not a regular java project you are working on. Android is totally different.
I will tell how it works. Open a emulator and wait for it to load. now,
Go to DDMs-> File Explorer
You will be able to see a folder by name "mnt" or "sdcard". Click on it. And now on the top right corner you will have three icons , one to push a file to sdcard and one to delete from sdcard and also one to pull a file from sdcard.
So click on the push a file icon and select your file and copy it to sdcard.
Now you have your file in sdcard. here you have to learn how to read a file from sdcard.
And I am sure there are so many examples available on internet on how you can read a file from sdcard.
Hope this will help you to get it started.
Here's how you can create your xml file.
File f = new File(Environment.getExternalStorageDirectory() + "/yourxmlfilename.xml");
// If file does not exist, clear log count
if(!f.exists())
f.createNewFile();
The problem is this:
I make an internet connection to some url and receive an HttpResponse with an app_example.apk.
Then I want to create a file (an .apk)
in the sdcard with this data so that this downloaded application
can be installed later.
How can I convert the HttpResponse to an .apk file?
Let's clear some details:
I have to get this apk file through an internet connection to my server
I don't want to install this applications I receive on the sdcard
All of this has to be done in my code, I cannot use android market
I am currently writing to that file.
What I'm doing is converting the HttpResponse to a byte[ ],
then that byte[ ] is written to a file (an .apk) using an ObjectOutputStream.
Like this:
// byte[] appByteArray - already has the internet response converted in bytes
try {
file = new File(Environment.getExternalStorageDirectory()+"/"+appName+".apk");
file.createNewFile();
FileOutputStream stream = null;
stream = new FileOutputStream(file, false);
ObjectOutputStream objectOut =
new ObjectOutputStream(new BufferedOutputStream(stream));
objectOut.writeObject(appByteArray);
objectOut.close();
} catch (Exception e) {
e.printStackTrace();
}
In the end, the file is created
and has the received content.
When I try to install it,
through a VIEW intent (using the default installer)
I get a parse error saying that it could not find the AndroidManifest.xml.
I think that in some step along the way, the received data is being corrupted.
Do you have another method to solve this?
Many thanks
Don't use an ObjectOutputStream, byte array is serialized as Object, not written as raw data.
Are you sure that you have SD card write permission? android.permission.WRITE_EXTERNAL_STORAGE
Don't write into SD card root directory. Number of files in root dir can be limited. Instead create you app subdirectory on SD CARD.
This code works for me:
try {
String filePath = Environment.getExternalStorageDirectory()
+ "/myappdir/" + appName + ".apk";
File file = new File(filePath);
file.getParentFile().mkdirs();
file.createNewFile();
BufferedOutputStream objectOut = new BufferedOutputStream(
new FileOutputStream(file));
objectOut.write(appByteArray);
objectOut.close();
} catch (Exception e) {
e.printStackTrace();
}
This may not be the core problem, but I don't think you want to wrap stream in an ObjectOutputStream, since that is used for object serialization. It could be that it is adding extra data to the file so it can be deserialized with ObjectInputStream.
I would try pulling the apk off of the emulator (or device) and check it's MD5 versus the file on the server to make sure that the bits are being written out correctly.
Take a look at Pavel P's answer.
Also, I would note that your idea of installing the APK using the VIEW intent action does work, as I have tested this technique in the past.
However, unless the user has explicitly gone into Settings → Applications and selected "Allow non-Market applications", your installation will fail and the user will just see a screen telling them that for security reasons the installation has been blocked.
Basically you really need to rely on having fairly tech-savvy users who are willing to overlook a scary security warning and go and disable that setting.