How to Copy the Internal Default ringtones to External Memory in Android - android

Please help me out I am not getting the default ringtone file path.
Can any body tell how to get access to the default ringtone in Android. Here is my code for doing that thing. I have commented the path that I gave directly to asset manager to open the file and read it.
public void copyAssets() {
AssetManager assetManager = this.getAssets();
// String FileName="//media/internal/audio/media/";
File io=getFilesDir();
String[] files = null;
try {
files = assetManager.list("");
// files=assetManager.list(FileName);
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for (String filename : files) {
InputStream in = null;
OutputStream out = null;
// File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
// File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
// FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual
//
try {
in = assetManager.open("Ringtone");
// File myFolder = new File(Environment.getDataDirectory() + "/myFolder");
File myFolder = this.getDir("myFolder", this.MODE_PRIVATE);
File fileWithinMyDir=new File(myFolder,"Ringtoness");
out = new FileOutputStream(fileWithinMyDir);
copyFile(in, out);
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
}
public void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}`

Related

Writting to root sdcard folder

I am trying to copy a file over to the root SD card on an Android device.
When I execute the code, it is going to the sdcard/Android/Data/<packageName>/files folder.
I have tried several different options without any success.
Also, I have checked the app to make sure it has write and read access to external memory.
private void copyfiles() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("ini");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (files != null)
for (String filename: files) {
InputStream in = null;
OutputStream out = null;
try { in = assetManager.open("sdcard/" + filename);
//File outFile = new File(Environment.getExternalStorageDirectory(), filename);
File outFile = new File(getExternalFilesDir(null), filename);
if (!(outFile.exists())) { // File does not exist...
out = new FileOutputStream(outFile);
pastefiles( in , out);
}
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
} finally {
if ( in != null) {
try { in .close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
}

How to save a text file present in asset folder of android app to the local directory of android phone as text or pdf file?

I have saved some text files in asset folder of android apk , whenever user opens the app which is having list of filenames ,if he clicks on a filename corresponding file opens from asset folder ,
Now I want to let user save that file in text or pdf format to his local directory so that he can transfer it or use it or modify it according to his choice
EDIT : I just want to know the api needed ,not the whole code
Use this function to copy something from assets to storage
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(null), filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
Reference : Move file using Java

copy files from assets folder to sd card in android

my code works fine for copying a file from assets folder to sd card. but whenever i try to copy again, it just replaces the old file with the new one instead of renaming it. how do i fix this? thanks
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(null), filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
}
Overwriting files in case they exist is a normal behavior, Use fileObject.exists() method to check, If the file exists pick another filename, You can use System.currentTimeMillis() method to gain a unique suffix and add it to the file name.
NaN answer is one of the ways to achieve your goal, just changing one line will do the job:
File outFile = new File(getExternalFilesDir(null), filename + System.currentTimeMillis());
Another way is to increase file size ad eternum, but if you are making a tools suit this won't fit your needs.

Copying file from asset folder to sdcard doesn't seem to work

I am trying to copy an image from the asset folder to the sdcard but doesn't seem to copy it on first launch. It creates the folder okay but doesn't copy the file over.
prefs = getPreferences(Context.MODE_PRIVATE);
if (prefs.getBoolean("firstLaunch", true)) {
prefs.edit().putBoolean("firstLaunch", false).commit();
File nfile=new File(Environment.getExternalStorageDirectory()+"/My Images");
nfile.mkdir();
}
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("middle.jpg");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(Environment.getExternalStorageDirectory()+ "/My Images" + filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
private void copyFile(InputStream in, OutputStream out) {
// TODO Auto-generated method stub
}
middle.jpg is the file i want to copy over. Can any one tell me what i am doing wrong?
PS i have WRITE_EXTERNAL_STORAGE in my manifest.
Thanks
You forgot to add / in the end of /My images while constructing the path
File outFile = new File(Environment.getExternalStorageDirectory()+ "/My Images/" + filename);
out = new FileOutputStream(outFile);
because the filename would be MyImages+Filename so it wouldn't exists for copying.

Install vCard using android app?

Is there any way to install a vCard using android app, as soon as it starts for the first time.
Although for running any block of code for the first time, I can use these lines ...
if (isFirstTime()) {
//First time code
}
and
private boolean isFirstTime()
{
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
}
return ranBefore;
}
but how will I be able to install a vCard from app.
Note: Although I have the vCard already made, and can be put in the raw directory.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(storage_path+vfile)),"text/x-vcard");
startActivity(intent);
Edit
Copy vcard to sdcard
private void copyAssets() { AssetManager assetManager = getAssets();
String[] files = null;
try { files = assetManager.list(""); } catch (IOException e)
{ Log.e("tag", "Failed to get asset file list.", e);
} for(String filename : files)
{
InputStream in = null; OutputStream out = null;
try { in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(null), filename);
out = new FileOutputStream(outFile); copyFile(in, out); } catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e); }
finally {
if (in != null) { try { in.close();
} catch (IOException e) { // NOOP }
} if (out != null) {
try { out.close(); } catch (IOException e) { // NOOP }
}
}
}
} private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024]; int read; while((read = in.read(buffer)) != -1){ out.write(buffer, 0, read);
} }
Then type the location where I have written storage_path

Categories

Resources