How to copy files in Android Storage? - android

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I am trying to copy .jpg file to and from in Storage folders.
try {
Process c = Runtime.getRuntime().exec("dd if="+path+
"of="+Environment.getExternalStorageDirectory()+"/1.jpg");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
path contains the exact image location in storage ! But it doesn't work. It can't copy the image to another folder of storage. Please kindly help me to solve this in every possible way! Thank you.

try {
String cmd="dd if="+path+" of="+Environment.getExternalStorageDirectory()+"/1.jpg";
Runtime.getRuntime().exec(cmd+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

It happens to me sometimes and I use
Environment.getExternalStorageDirectory().getPath()
Have you tried that?

Related

How to check a file before delete in android

I know how to delete a file, it's:
File file = new File(path);
file.delete();
Can I test that the file is currenlty in use without rooting my device?
For exmple I want to check if the file is open before i can delete it.
I want to be able to catch errors with a try catch sentence.
You can use a try/catch clause.
try {
Files.delete(path);
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
System.err.format("%s not empty%n", path);
} catch (IOException x) {
// File permission problems are caught here.
System.err.println(x);
}
You can find more information here.

MediaPlayer - can't find path of sdcard on real device

On my code I use:
mp = new MediaPlayer();
String filePath = Environment.getExternalStorageDirectory().getPath() + "/mymusic/asong.mp3";
try {
mp.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
And on the emulator the song.mp3 is played normally. But when I test it on my real device, it gives an error (-38, 0). That means it can not find the path of the song. I connect the usb cable, go to my Computer, GT-I8260 and paste the folder "mymusic" (that contains asong.mp3) under "Card" folder (where an empty folder named "LOST.DIR" is also placed). But why doesn't it work? Thanks a lot
it's card but at least
Environment.getExternalStorageDirectory() + "/mymusic/asong.mp3";
is enough.
Make sure it exists because you may not have the folder created before.
File f = new File(Environment.getExternalStorageDirectory() + "/mymusic");
if (!f.exists()) { f.mkdirs(); }
also make sure that it's not mounted while writing, since it may happen that it is not accessable at all.
Also revalidate that you have setupted the manifests permission to read/write the external storage

Delete files starting with particular word

In my application some temporary files are created,and its name starts with 'temp'. How to delete these files from sd card on exit .
you can delete files from SDCARD as:
File folder = new File(Environment.getExternalStorageDirectory() + "/tempdirname");
try {
File[] filenamestemp = folder.listFiles();
for(int i=0;i<filenamestemp.length;i++){
if(filenamestemp[i].getAbsolutePath().toString().contains("temp"))
filenamestemp[i].delete();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and make sure you have added SDCARD access permission in AndroidManifest.xml :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

created images not recognized in android gallery

I'm creating an app that creates and saves images. Currently I am saving them to /sd_card/my_app_name/. The files are there and everything, but they are not recognized from the Android's gallery. Is there any way to fix this?
Thanks!
well, I figured this out quickly enough :
try {
MediaStore.Images.Media.insertImage(getContentResolver(), filePath, "image name", "image dexscription"); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Use below code to make every new change recognized.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
I think this will resolve your issue.
The way to do this is this:
MediaStore.Images.Media.insertImage(getContentResolver(), filePath, image name, details);

Android bitmap in path

I am wondering how to get the bitmap from specific location as follows:
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("C:/java/AVMOrderServer/files/main_ads/fff.jpg").getContent());
iv.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Any advice would be useful (I found the code sample on stackoverflow which is an answer voted as true)
Thank you all.
Your Android device does not have a C drive, that's generally a DOS/Windows concept. Android is based on Linux. If you want to view a bitmap on your computer from your Android device you'll have to copy it over first, whether by including it in your APK or by copying to SD card.

Categories

Resources