I am writing out a file to the external storage on android. The file shows up in my device when I browse to location using the ap ES file explorer.
But when I plug my device in to windows, the file does not show up.
Furthermore if I write an empty file named "test.wav" to Ringtones folder, no test.wav will show up in my ringtones settings.
But, if I create an empty file in windows named "test.wav", and drag and drop into my Ringtones folder, it will then properly show up in my ringtones browser in settings.
Code I am using is a follows.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_RINGTONES), "testfile.wav");
file.setReadable(true);
try {
FileOutputStream fileOut = new FileOutputStream(file);
stream = new DataOutputStream(fileOut);
stream.flush();
stream.close();
//this code successfully creates a file
// but file is not viewable by all means
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Android uses tables to keep track of images, videos, sounds and so on these tables called " content providers " for example it uses MediaStore.Images.Media.DATA for images Note that these tables are used for the view this illustrates why you can only see it using ES. When you add new file manually, you need to add an entry for this file in the corresponding table or refresh your music library in case of sound file was added, but when you add file using windows the device's driver updates its tables automatically. Try refreshing your music application or even restarting your phone if this solves the problem updating your content provider is the solution.
I could be wrong but I think that it is /extsd showing when you plug your device into Windows, whereas external storage is actually /sdcard. so your file is in /sdcard, not in /extsd, that's why you don't see it. At least whenever I plugged Android tab into Windows, I could only browse its removable SD, i.e. /extsd but I couldn't see /sdcard
Related
I'm recently starting with codenameone and I'm working on a music player app. I want to get the musics from a folder. I found out in the documentation a code about "Audio Capture & Recording".
But what I'm looking for is to just play audios from a specific folder.How can I adjust this code? I'm also not finding where these audios are being saved to.
Here's the code+ a footage of the Recording code.
Button button = new Button("musiques!");
Form hi = new Form("musiques", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
hi.addComponent(BorderLayout.CENTER, LayeredLayout.encloseIn(BoxLayout.encloseY(button)));
button.addActionListener((e) -> {
InputStream streamFromResource = CN.getResourceAsStream("/filename.mp3");
try {
MediaManager.createMedia(streamFromResource, "audio/mp3");
} catch (IOException ex) {
System.out.print("non");
}
});
The audio is in fs.getAppHomePath() + "recordings/". Bundling the audios into the jar will increase your jar size and final app size which will exceed the free quota so we don't normally recommend that.
If you still choose to go with that route you can store the files in the root of the src folder and access them using CN.getResourceAsStream("/filename.mp3").
Then you can just use:
Media m = MediaManager.createMedia(streamFromResource, "audio/mp3");
Alternatively you can store the files on the web and use an HTTPS URL instead of a file URL to play them.
You can't dynamically list the files since there's no listing option for HTTP or for the contents of the jar.
I've seen this question, it's not a duplicate.
I'm pretty new to Android-Development, so my way to store personal data on the device might be not the best one.
However, when I update the App, the stored files aren't found anymore.
File path = myContext.getFilesDir();
File pathUse = new File(path, "lists.ser");
if(!pathUse.exists()) {
try {
pathUse.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try(ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(pathUse))) {
os.writeObject(myObject);
} catch(Exception e) {
e.printStackTrace();
}
How can I store the data that way, that it is kept after an update?
Internal files are removed only if you uninstall and reinstall. An upgrade will not remove internal files -- and it shouldn't. Consider that if it did that, your database would be removed for every upgrade as well.
Check the official documentation for more Info Click here
Hence storing the application specific data in external storage is not recommended because anyone can access it.
I'm trying to copy a file that is located in the External storage directory into a directory that is in my SD Card. However, when I check to see if the file has successfully been copied, the file is not even created in the SD Card.
Am I missing something? Here is the code I have:
String sourcePath = Environment.getExternalStorageDirectory().getPath() + newFileName;
File source = new File(Environment.getExternalStorageDirectory().getPath(), newFileName);
String destinationPath = "/storage/external_SD";
File destination = new File(destinationPath, newFileName);
try {
if(!destination.exists()){
destination.mkdir();
}
FileUtils.copyFile(source, destination);
} catch (IOException e) {
e.printStackTrace();
}
The copyFile method is from an Apache library. Here is the link for it: https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
However, when I check to see if the file has successfully been copied, the file is not even created in the sd Card.
You do not have arbitrary filesystem-level access to removable storage on Android 4.4+.
Is there a work around for this?
That depends on what your objective is.
If you insist that you must be able to write to that specific path on arbitrary user devices... then, no, there is no supported workaround. After all, there is no /storage/external_SD on the vast majority of Android devices. Where and how device manufacturers choose to mount removable media is up to them and is an implementation detail that will vary.
If you relax that restriction, but insist that you must be able to write a file to the root directory of removable storage on arbitrary user devices... then, no, there is no supported workaround today. The N Developer Preview has a "Scoped Directory Access" feature that should allow this, but it will be several years before you can assume that an arbitrary user device will be running that version of Android or higher. Also, you do not get actual filesystem access, but rather a Uri (see the Storage Access Framework option, below).
Now, if you are more flexible about the precise location, you have other options:
You can use getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs(), all methods on Context. Note the plural form. If those return 2+ entries, the second and subsequent ones are locations on removable storage that you can read from and write to, no permissions required. However, you do not get to choose the exact path. And if the device has 2+ removable storage volumes, I'm not quite certain how you would help the user tell them apart.
You can use the Storage Access Framework and let the user choose where to put the file. The user is welcome to choose removable storage... or not. You get a Uri back, which you can use with ContentResolver and openOutputStream() to write your content. You can also take persistable Uri permissions so you can work with that file again in the future, assuming the user doesn't move or delete it behind your back.
If you want to copy to external storage then you need
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The destinationPath you mentioned may not be accessible as it may belong to the private system folders or some other application folders. You can however use public folders like Pictures,Music, Videos,Downloads,etc. or create sub folders inside them -
String sourcePath = Environment.getExternalStorageDirectory().getPath() + newFileName;
File source = new File(Environment.getExternalStorageDirectory().getPath(), newFileName);
File destinationPath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS, "/external_SD");
try {
if(!destinationPath.exists()){
destinationPath.mkdir();
}
File destination = new File(destinationPath, newFileName);
FileUtils.copyFile(source, destination);
} catch (IOException e) {
e.printStackTrace();
}
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 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.