This question already has answers here:
Android How to use MediaScannerConnection scanFile
(8 answers)
Closed 8 years ago.
Im working on Android. My android application successfully creates a text file in SD card memory; the created text file can be seen in the DDMS explorer but cannot be seen in the windows explorer. Here is my code:
private void initFile(String filename, char[] data, int length){
File File = new File(Environment.getExternalStorageDirectory() + File.separator + "Download" + File.separator + filename);
try {
File.delete();
File.createNewFile();
FileOutputStream fOut = new FileOutputStream(mFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
if(mFile.exists())
{
myOutWriter.write(data,0,length);
myOutWriter.flush();
myOutWriter.close();
fOut.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Note: But if I restart the android the created text file can be seen already in the windows explorer.
You have any idea why is it so? Why the file cannot be seen in the windows explorer when it is created in android when it can be seen in the DDMS?
Im using real android tablet.
You have to pull the file from DDMS File Explorer to Windows File Explorer. You have to select the file on the DDMS File Explorer and then click the button pull a file from the device present at the right side of the DDMS File Explorer Window and save it in any of the folder on your wish. After that you can see it in Windows FileExplorer
Related
so i know there are alot of questions about creating a text file in android but i cant find any answers to my specific questions.
So the device I'm using is not an phone or a tablet, its a controller with android on it so the file directory is getting me confused. i want to create a new file either on the sd card or the device itself (it doesn't really matter as long as i can see it). firstly, the device doesn't have google docs or sheets or anything like that, will i need to install one of these apps on it for it to be able to read .txt files? or does android have some sort of internal software to do that?
Secondly, i've found alot of code on how to create a new file but most of it seems to gloss over how to get file directory, is there a specific way i can get the file directory?
This is what im using so far and im not getting any errors but its also not creating any files (or at-least none that i can see).
public void WriteToText(){
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = null;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
1, I think all devices have Html viewer which can view the text files. If you click a text file in a file manager app, it should prompt you with a list of apps that can view the text file.
2, Your code is correct, but it creates a file in the app's private file directory, which is not visible to all other apps, including file manager apps. This is because of Android security policy to prevent any app to steal information from other apps.
If you want to write a file which is visible to other apps like file manager, there are 2 ways.
in old api level (<29?) you can get the get external storage with
Environment.getExternalStorageDirectory()
you will need to request permission for it.
save the file in your app's private directory like what you have done, then create a chooser to share it out with correct mime type (txt/plain?), it will prompt you to select app that can view this mime type like what file manager does.
I have tried the followings for .txt, .sql, .java with success BUT I can't write file with the extension with .pdf, .jpg, .zip.
File file = new File("d:/myFolder/something.txt");
File file = new File("d:/myFolder/something.sql");
File file = new File("d:/myFolder/something.java");
// Executed Successfully for the above 3 but java.io.FileNotFoundException (Access is denied) show for these files below.
File file = new File("d:/myFolder/something.jpg");
File file = new File("d:/myFolder/something.pdf");
File file = new File("d:/myFolder/something.zip");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(mpf.getBytes());
fos.close();
fos.flush();
} catch (Exception e) {
System.out.println(e.getMessage());
}
Project is being developed using spring MVC on Windows 7 (64 bit).
Perhaps there is an unusual antivirus or similar security product running on your Windows?
If so, either disable the antivirus or configure it to permit these filetypes.
So long as it is enabled, it is actually doing exactly what it is supposed to do by blocking these file-writes.
This question already has answers here:
How can I download and save a file from the Internet using Java?
(23 answers)
Closed 4 years ago.
I want to add a URL into my java program: http://www.markit.com/news/InterestRates_JPY_20160426.zip; so basically when you open this link a zip file is downloaded. How do I do that?
And then, I want to unzip the downloaded file in the java program as well.
How do I do these in java?
You can use zip4j to unzip your file.
To download a file in Java you can use this code.
try
{
String url = "download url";
String path = "C:/Users/...."; // Path to where the files is going to be downloaded.
ReadableByteChannel in = Channels.newChannel( new URL(url).openStream() );
FileOutputStream fileOutputStream = new FileOutputStream(path);
FileChannel out = fileOutputStream.getChannel();
out.transferFrom(in, 0, Long.MAX_VALUE);
}
catch (Exception e)
{
e.printStackTrace();
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In java under Windows, how do I find a redirected Desktop folder?
How to get the Desktop path in java
I want to write my results to the desktop of the user rather than to the same directory as file class that I am running.
I am using Mac OS.. How about in Window?1
Thanks
The user's home directory is:
System.getProperty("user.home")
In general +"/Desktop" would do, but is not portable.
String userHomeFolder = System.getProperty("user.home");
File textFile = new File(userHomeFolder, "mytext.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(textFile));
try {
...
} finally {
out.close();
}
This would write the file "mytext.txt" to the home directory.
Hi I want to make images invisible to android gallery or any third party gallery app, the image will be places in specific folder on SD card.
For example I have following code to save an image to a folder called myimages. I just want the images stored in myimages folder should not be visible to any gallery app and only my own application can access these images.
void saveBitmap(Bitmap bmp)
{
FileOutputStream os;
String dirName = "/mvc/mvc/myiamges/";
try {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)){
String root = Environment.getExternalStorageDirectory()
.toString();
File dir = new File (root + dirName);
boolean created=dir.mkdirs();
//File file = new File(this.getExternalFilesDir(null),
// this.dirName+fileName);
//this function give null pointer exception so im
//using other one
File file = new File(dir, "aeg2.png");
os = new FileOutputStream(file);
}else{
os = openFileOutput("aeg2.png", MODE_PRIVATE);
}
bmp.compress(CompressFormat.PNG, 100, os);
os.flush();
os.close();
}catch(Exception e){
e.printStackTrace();
}
}
Rename those files with custom extensions like filename.extension.customextension
like hello.avi.topsecret.
When you need the file to be ready mode to play rename it to proper extension, play and rename it back.
This should work for you.
or
Prefix your folder name with a dot "."
Check these links for more info:
http://www.makeuseof.com/tag/hide-private-picture-folders-gallery-android/
Yes, save it with any extension you want or even without extension.
In your app, just read it as normal image file.
Create an empty file inside your image store folder named '.nomedia' <- atention to the initial point.
All media files sabed inside this folder will not be showed in galery browsers.