(File export)Is it a correct code?[Need help]
(I'm beginning in the Android dev) I want to export a file from /data/data.... to a directory in the SDCARD, here is my code:
public class Import extends Activity {
private Context context;
public void transfer(){
context = getApplicationContext();
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/SDCARD/Carburant");
dir.mkdirs();
copyfile(context,"/data/data/carburant.android.com/files/","/SDCARD/Carburant/storeddata.dat");
}
private void copyfile(Context context,String srFile, String dtFile){
context = getApplicationContext();
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream 开发者_JAVA百科in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
Toast.makeText(context, "Export effectué", Toast.LENGTH_SHORT).show();
}
catch(FileNotFoundException ex){
Toast.makeText(context, "File Not found", Toast.LENGTH_SHORT).show();
}
catch(IOException e){
Toast.makeText(context, "Echec", Toast.LENGTH_SHORT).show();
}
}
}
Edited: 1st try:
case R.id.importer:
String name;
String mark;
Import myImport = new Import(this,name,mark);
Import myImport(this, name, mark);
myImport.transfer();
return true;
i have an error on this line : Import myImport(this, name, mark);
Import cannot be resolved to a variable
The method myImport(Saves, String, String) is undefined for the type Saves
Thank you for your help.
Change your Import class as follows (remove extends Activity)...
public class Import {
private Context context;
private String name;
private String mark;
public Import(Context context, String name, String mark) {
this.context = context;
this.name = name;
this.mark = mark;
}
// Your other methods here
}
In the Activity which you use to create an instance of your Import class use...
Import myImport = new Import(this);
samold's suggestion of using...
copyfile(context,"/data/data/carburant.android.com/files/",
sdCard.getAbsolutePath() + "/SDCARD/Carburant/storeddata.dat");
...will probably work OK although I'm worried about your source path...
"/data/data/carburant.android.com/files/"
...the above doesn't make sense for two reasons. Firstly you're not specifying a source filename (only a directory path). Secondly, app data is stored in a path as follows...
/data/data/<package name>
...package name should be similar to com.mycompany.myApp - you have it the other way around (i.e., myApp.mycompany.com) so unless carburant.android.com really IS the name of your package this isn't going to work as that directory won't exist.
EDIT: When the user enters their name and car mark just save them as Strings, e.g.,
String name;
String mark;
Then construct the path to file as ...
"/data/data/carburant.android.com/files/" + name + "." + mark + ".settings.dat;
To use your Import class, don't use startActivity with an Intent (remember it's not an Activity now). In the code where you check for the button press, simply use...
Import myImport(this, name, mark);
myImport.transfer();
See my modified code example for the Import constructor to take the name and mark strings.
One more thing, remove the line where you have...
context = getApplicationContext();
...this isn't necessary and will cause problems.
I think the problem is here:
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/SDCARD/Carburant");
dir.mkdirs();
copyfile(context,"/data/data/carburant.android.com/files/",
"/SDCARD/Carburant/storeddata.dat");
You create the directories with the absolute path of the SD card prepended to "/SDCARD/Carburant/"
, but when you call your copyfile()
routine, you forgot the absolute path of the SD card.
Try this:
copyfile(context,"/data/data/carburant.android.com/files/",
sdCard.getAbsolutePath() + "/SDCARD/Carburant/storeddata.dat");
精彩评论