lundi 2 mars 2015

Android: File and Folder Transfer from Internal SD card to External SD mounted card programmatically


Hi I am making a small application to move directory and files from Installed memory to external mounted SD card. But its behavior is crazy. The same application running fine for Samasung Galaxy S2 but not on Micromax canvas 3d.


here is the function copyRecursive(String,String)



public void copyRecursive(String intPath,String extPath) { // recursive function
File intRoot = new File(intPath);
File dir1 = new File (intRoot.getAbsolutePath()); // get into internal sd card

File list[] = dir1.listFiles(); // get all files name in the directory
for( int iter=0; iter< list.length; iter++)
{
if(list[iter].isDirectory()) // check if file is a directory
{

try
{
File f= new File(extPath);
if(!f.isDirectory()) //check for inserted memory card
{
final Dialog dialog1 = new Dialog(Display.this);
dialog1.setContentView(R.layout.email_dialog);
dialog1.setTitle("Error");
EditText et2= (EditText)dialog1.findViewById(R.id.textbox);
et2.setText("no memory card");
Button bt = (Button)dialog1.findViewById(R.id.button);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

dialog1.dismiss();
return;

}

});
dialog1.show();
return;
}
File extRoot1 = new File(extPath+"/"+list[iter].getName());
File dir2 = new File (extRoot1.getAbsolutePath()); // make file

if(!dir2.exists())
{
dir2.mkdir(); // make directory if directory doesnot exist on exteranl sd mounted card
}

}catch(Exception e)
{
e.printStackTrace();


}
copyRecursive(intPath+"/"+list[iter].getName(),extPath+"/"+list[iter].getName()); // call recursive
}
else
{
try
{
File extRoot2 = new File(extPath+"/"+list[iter].getName());
File dir21 = new File(intPath+"/"+list[iter].getName());
InputStream in = new FileInputStream(dir21);
OutputStream out = new FileOutputStream(extRoot2);
if(!extRoot2.exists())
{
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) { // write bytes ti the created file
out.write(buf, 0, len);
}
in.close();
out.close();


}
}
catch(Exception e)
{

e.printStackTrace();

}

}

}

return;

}

}


the Parameter to the functions which are the path to the memory directory are hard coded as



public String int_card_path=Environment.getExternalStorageDirectory()+"/Android/data/com.handygo.rockasapv"; // it gives the internal memory directory

public String ext_card_path="/storage/"+getSDCardPath()+ "/Android/data/com.handygo.rockasap"; // it gives external memory mounted directory path


the function getSDCadPath() is defined as follow. It gives the name of the card mounted.



private String getSDCardPath() {
// TODO Auto-generated method stub
String[] dirList = null;
File storageDir = new File("/storage/");
if(storageDir.isDirectory()){
dirList = storageDir.list();
//TODO some type of selecton method?
}
return dirList[0];
}


dirList[0] return the name of the card that is sdcard1


please help me out. sorry if this is duplicate but i dont find relevant answer to my problem





Aucun commentaire:

Enregistrer un commentaire