Copying Raw File into Sdcard

Copying raw file into SDCard?

Read from the resource, write to a file on the SD card:

InputStream in = getResources().openRawResource(R.raw.myresource);
FileOutputStream out = new FileOutputStream(somePathOnSdCard);
byte[] buff = new byte[1024];
int read = 0;

try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}

How to Copy Raw files into SD Card

Replace your code with mine.

it is working perfect here.

Add only this Permission :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Java Code :

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;

public class Sample extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int[] mSongs = new int[] { R.raw.background, R.raw.background1, R.raw.background2 };
for (int i = 0; i < mSongs.length; i++) {
try {
String path = Environment.getExternalStorageDirectory() + "/PriyankaChopra";
File dir = new File(path);
if (dir.mkdirs() || dir.isDirectory()) {
String str_song_name = i + ".mp3";
CopyRAWtoSDCard(mSongs[i], path + File.separator + str_song_name);
}
} catch (IOException e) {
e.printStackTrace();
}
}

}

private void CopyRAWtoSDCard(int id, String path) throws IOException {
InputStream in = getResources().openRawResource(id);
FileOutputStream out = new FileOutputStream(path);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
}

}

Copying directories and files from res/raw folder to sd card - android

I suggest you the keep your files in assets. The following code may help you to copy contents from assets directory to SD Card.

public static void copyFile(Activity c, String filename) 
{
AssetManager assetManager = c.getAssets();

InputStream in = null;
OutputStream out = null;
try
{
in = assetManager.open(filename);
String newFileName = sdcardpath/filename;
out = new FileOutputStream(newFileName);

byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Utility.printLog("tag", e.getMessage());
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
printLog(TAG, "Exception while closing input stream",e);
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
printLog(TAG, "Exception while closing output stream",e);
}
}
}
}

Android - Copy res/raw resource to SD correctly

As you are using the raw folder, simply use this from your activity:

getResources().openRawResource(resourceName)

Copy a file from res/raw to SDcard to send using Intent.ACTION_SEND

Try adding write external storage permission in the AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Copy file from raw dir to SDcard

Here is what you can use to do that :

InputStream in = getResources().openRawResource(R.raw.myresource);
FileOutputStream out = new FileOutputStream(somePathOnSdCard);
byte[] buff = new byte[1024];
int read = 0;

try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();

out.close();
}

Android : Copy RawFile to Sdcard (video mp4)

If you use an InputStream to read, use an OutputStream to write, i.e. a BufferedOutputStream-wrapped FileOutputStream. Also, your code is pretty inefficient, as it only copies one byte at a time. I'd suggest creating a byte array buffer and using these relevant read/write methods:

int BufferedInputStream.read(byte[] buffer, int offset, int length)
void BufferedOutputStream.write(byte[] buffer, int offset, int length)


Related Topics



Leave a reply



Submit