I'm exporting a file in the sdcard, however, I'm facing a FileNotFound Exception (04-12 01:26:18.494: DEBUG/Carburant(4568): /mnt/sdcard/Carburant/alaa.peugeot.settings.dat/alaa.peugeot.settings.dat (Is a directory)
)here is the code:
try {
File sdCard = Environment.getExternalStorageDirectory();
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
Log.d("Carburant", "Sdcard can read/write !!");
mExternalStorageAvailable = mExternalStorageWriteable = true;
try {
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String fileName = context.getResources().getString(
R.string.fileName);
String fileDir = "" + preferences.getString("login", "")
+ "." + preferences.getString("marque", "") + ".";
File f2 = new File(context.getFilesDir(), fileDir
+ fileName);
String y = f2.getAbsolutePath();
Log.d("HI Export", y);
InputStream in = new FileInputStream(f2);
File dir = new File(sdCard.getAbsolutePath()
+ "/Carburant/");
String x = dir.getAbsolutePath();
Log.d("HI", x);
File file = new File(dir, fileDir + fileName);
file.mkdirs();
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
// out.flush();
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();
String x = ex.getMessage();
Log.d("Carburant", x);
} catch (IOException e) {
Toast.makeText(context, "Echec", Toast.LENGTH_SHORT).show();
}
}
// copyfile(nom,file.getAbsolutePath());
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
Log.d("Carburant", "Sdcard only read !!");
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states,
// but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
} catch (Exception e) {
Log.d("CARBURANT", e.getMessage());
}
Want to export a file from /data/data/<package name>/fileDir+fileName to a directory Carburant in the sdcard.
File file = new File(dir, fileDir+fileName);
file.mkdirs();
I think you have created a directory called /mnt/sdcard/Carburant/alaa.peugeot.settings.dat/alaa.peugeot.settings.dat by mistake and now the code cannot over write it?
Related
I am loading 10 Images from URL's that are stored in array . only last index url is loading and saving image in SD card . here is my code :
for(int j=0; j<List.size();j++)
{
reviewImageLink =List.get(j).get(TAG_Image).toString();
URL reviewImageURL;
String name = reviewImageLink;//.substring(reviewImageLink .lastIndexOf("/") + 1,reviewImageLink.length());
try {
reviewImageURL = new URL(reviewImageLink);
if (!hasExternalStoragePublicPicture(name)) {
isImage = false;
new DownloadImageTask().execute(reviewImageURL);
Log.v("log_tag", "if");
isImage = true;
File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.directory));
//if(!sdImageMainDirectory.exists()){
sdImageMainDirectory.mkdirs();
File file = new File(sdImageMainDirectory, name);
Log.v("log_tag", "Directory created");}
}
catch (MalformedURLException e) {
Log.v(TAG, e.toString()); }
}
}//try
catch (Exception e) {
e.printStackTrace();}
DownloaderTask:
class DownloadImageTask extends AsyncTask<URL, Integer, Bitmap> {
// This class definition states that DownloadImageTask will take String
// parameters, publish Integer progress updates, and return a Bitmap
protected Bitmap doInBackground(URL... paths) {
URL url;
try {
url = paths[0];
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int length = connection.getContentLength();
InputStream is = (InputStream) url.getContent();
byte[] imageData = new byte[length];
int buffersize = (int) Math.ceil(length / (double) 100);
int downloaded = 0;
int read;
while (downloaded < length) {
if (length < buffersize) {
read = is.read(imageData, downloaded, length);
} else if ((length - downloaded) <= buffersize) {
read = is.read(imageData, downloaded, length- downloaded);
} else {
read = is.read(imageData, downloaded, buffersize);
}
downloaded += read;
publishProgress((downloaded * 100) / length);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,length);
if (bitmap != null) {
Log.i(TAG, "Bitmap created");
} else {
Log.i(TAG, "Bitmap not created");
}
is.close();
return bitmap;
} catch (MalformedURLException e) {
Log.e(TAG, "Malformed exception: " + e.toString());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.toString());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.toString());
}
return null;
}
protected void onPostExecute(Bitmap result) {
String name = reviewImageLink.substring(reviewImageLink.lastIndexOf("/") + 1,reviewImageLink.length());
if (result != null) {
hasExternalStoragePublicPicture(name);
saveToSDCard(result, name);
isImage = true;
} else {
isImage = false;
}
}
}
public void saveToSDCard(Bitmap bitmap, String name) {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
Log.v(TAG, "SD Card is available for read and write "+ mExternalStorageAvailable + mExternalStorageWriteable);
saveFile(bitmap, name);
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Log.v(TAG, "SD Card is available for read "+ mExternalStorageAvailable);
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
Log.v(TAG, "Please insert a SD Card to save your Video "+ mExternalStorageAvailable + mExternalStorageWriteable);
}
}
private void saveFile(Bitmap bitmap, String name)
{
String filename = name;
ContentValues values = new ContentValues();
File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.directory));
sdImageMainDirectory.mkdirs();
File outputFile = new File(sdImageMainDirectory, filename);
values.put(MediaStore.MediaColumns.DATA, outputFile.toString());
values.put(MediaStore.MediaColumns.TITLE, filename);
values.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis());
values.put(MediaStore.MediaColumns.MIME_TYPE, "output.jpeg");
Uri uri = this.getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
//Uri result = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
try
{
OutputStream outStream = this.getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 95, outStream);
outStream.flush();
outStream.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
private boolean hasExternalStoragePublicPicture(String name) {
File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.directory));
File file = new File(sdImageMainDirectory, name);
if (file != null)
{
file.delete();
}
return file.exists();
}
Pass the list to DownloadImageTask instead of URL then put a for loop in doInBackground,
This code for saving pictures on /Android/data/com.ocr.id/files/IDOCR/Number/
public static File save(Activity activity, Bitmap bm, String name) {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
File number = null;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
OutputStream outStream = null;
File externalFilesDir = activity.getExternalFilesDir(null);
File outFile = new File(externalFilesDir, "IDOCR" + File.separator
+ "Numbers");
if (!outFile.exists())
outFile.mkdirs();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
number = new File(outFile, timeStamp + "_" + name + ".PNG");
try {
outStream = new FileOutputStream(number);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
outStream = null;
bm.recycle();
// bm = null;
System.gc();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Toast.makeText(activity, "Your SDcared is read only.",
Toast.LENGTH_LONG).show();
} else {
// Something else is wrong. It may be one of many other states, but
// all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
Toast.makeText(activity, "Your SDcared is unmounted.",
Toast.LENGTH_LONG).show();
}
return number;
}
What's is the error in that code which throw a null pointer exception as this line
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
This code was working with me before but now throwing me an exception.
Try add the user permisson in your XML file!
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I have a code that downloads an image from a website and saves it on the SD-card.
When I send 2 web addresses for some reason, it only downloads the first image 2 times and saves them both with the second image name so if there is image1 and image2 on the website the code will only download image1 two times and save it on SD-card as image2 can please someone tell me what am I doing wrong?
public String getLocalLink(String image_URL, String imageName){
/** This is what we do with this method:
* Go online, according to the link, get the content, call the method to save.
*/
ImageLink = image_URL;
URL ImageLinkURL;
try {
ImageLinkURL = new URL(ImageLink);
//URL url = new URL(strURL);
if (!hasExternalStoragePublicPicture(imageName)) {
isImage = false;
new DownloadImageTask().execute(ImageLinkURL);
Log.v("log_tag", "if");
isImage = true;
File sdImageMainDirectory = new File(Environment
.getExternalStorageDirectory(), getResources()
.getString(R.string.directory));
sdImageMainDirectory.mkdirs();
File file = new File(sdImageMainDirectory, imageName);
Log.v("log_tag", "Directory created");
}
} catch (MalformedURLException e) {
Log.v(TAG, e.toString());
}
return ("/sdcard/Hanud/”+imageName+".jpg");
}
private class DownloadImageTask extends AsyncTask<URL, Integer, Bitmap> {
// This class definition states that DownloadImageTask will take String
// parameters, publish Integer progress updates, and return a Bitmap
protected Bitmap doInBackground(URL... paths) {
URL url;
try {
url = paths[0];
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
int length = connection.getContentLength();
InputStream is = (InputStream) url.getContent();
byte[] imageData = new byte[length];
int buffersize = (int) Math.ceil(length / (double) 100);
int downloaded = 0;
int read;
while (downloaded < length) {
if (length < buffersize) {
read = is.read(imageData, downloaded, length);
} else if ((length - downloaded) <= buffersize) {
read = is.read(imageData, downloaded, length
- downloaded);
} else {
read = is.read(imageData, downloaded, buffersize);
}
downloaded += read;
publishProgress((downloaded * 100) / length);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,
length);
if (bitmap != null) {
Log.i(TAG, "Bitmap created");
} else {
Log.i(TAG, "Bitmap not created");
}
is.close();
return bitmap;
} catch (MalformedURLException e) {
Log.e(TAG, "Malformed exception: " + e.toString());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.toString());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.toString());
}
return null;
}
protected void onPostExecute(Bitmap result) {
String name = ImageLink.substring(ImageLink
.lastIndexOf("/") + 1);
if (result != null) {
hasExternalStoragePublicPicture(name);
saveToSDCard(result, name);
isImage = true;
} else {
isImage = false;
}
}
}
public void saveToSDCard(Bitmap bitmap, String name) {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
Log.v(TAG, "SD Card is available for read and write "
+ mExternalStorageAvailable + mExternalStorageWriteable);
saveFile(bitmap, name);
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Log.v(TAG, "SD Card is available for read "
+ mExternalStorageAvailable);
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
Log.v(TAG, "Please insert a SD Card to save your image "
+ mExternalStorageAvailable + mExternalStorageWriteable);
}
}
private void saveFile(Bitmap bitmap, String name) {
String filename = name;
ContentValues values = new ContentValues();
File sdImageMainDirectory = new File(Environment
.getExternalStorageDirectory(), getResources().getString(
R.string.directory));
sdImageMainDirectory.mkdirs();
File outputFile = new File(sdImageMainDirectory, filename);
values.put(MediaStore.MediaColumns.DATA, outputFile.toString());
values.put(MediaStore.MediaColumns.TITLE, filename);
values.put(MediaStore.MediaColumns.DATE_ADDED, System
.currentTimeMillis());
values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
Uri uri = this.getContentResolver().insert(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
try {
OutputStream outStream = this.getContentResolver()
.openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.PNG, 95, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean hasExternalStoragePublicPicture(String name) {
File sdImageMainDirectory = new File(Environment
.getExternalStorageDirectory(), getResources().getString(
R.string.directory));
File file = new File(sdImageMainDirectory, name);
if (file != null) {
file.delete();
}
return file.exists();
}
I have made a test on your case. My example is very simple, I put a button on the stage. When I click the button, I will start two asynctask to download two pictures. I am creating a folder in my sdcard. After testing, I can get two pictures in the folder.
After I check your code, I guess you have a class variable ImageLink, which is assigned twice when you call getLocalLink twice. So the image will be stored in your second image name file.
You can check my example, it works for your requirement. You can create multiple asyncTask to download multiple images. Please just check the AsyncTask part.
http://jmsliu.com/1929/android-progress-dialog-example.html
Merged with android download image and then read it from sd-card using sqlite.
hi to all i have this problem with my code and i tried every thing that i know of and i posted it on some forums and didnt get an answer im new to android and im still learning .....
my question is that i have a code that is suppose to receive a variable that contains a website that has an image so this variable changes every time i send a new link this code should go online and download the image and save it to the sd-card then i read it and display it
so my problem with the code is if im sending 2 links to it, it downloads 1 of the images and it always stores it with the second image name (example: im sending image1 and image2 the code downloads image1 two times and stores it as "image2") when i mount the sd-card and check the image directory there is only 1 image there named image2,
i thought that doInBackground was causing the problem but im also using onPostExecute()
so please if someone can help me i would be thankful for his help
Note this is how i call it:
locLink = getLocalLink(buffer[3], buffer[0]);
buffer[3] = image website (example:http://www.oceanwideimages.com/images/7049/large/24M2444-16-palm-fringed-tropical-island.jpg)
buffer[0] = new file name (example 1 as a string)
this is the code:
private class DownloadImageTask extends AsyncTask<URL, Integer, Bitmap> {
// This class definition states that DownloadImageTask will take String
// parameters, publish Integer progress updates, and return a Bitmap
protected Bitmap doInBackground(URL... paths) {
URL url;
try {
url = paths[0];
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
int length = connection.getContentLength();
InputStream is = (InputStream) url.getContent();
byte[] imageData = new byte[length];
int buffersize = (int) Math.ceil(length / (double) 100);
int downloaded = 0;
int read;
while (downloaded < length) {
if (length < buffersize) {
read = is.read(imageData, downloaded, length);
} else if ((length - downloaded) <= buffersize) {
read = is.read(imageData, downloaded, length
- downloaded);
} else {
read = is.read(imageData, downloaded, buffersize);
}
downloaded += read;
publishProgress((downloaded * 100) / length);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,
length);
if (bitmap != null) {
Log.i(TAG, "Bitmap created");
} else {
Log.i(TAG, "Bitmap not created");
}
is.close();
return bitmap;
} catch (MalformedURLException e) {
Log.e(TAG, "Malformed exception: " + e.toString());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.toString());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.toString());
}
return null;
}
protected void onPostExecute(Bitmap result) {
String name = ImageLink.substring(ImageLink
.lastIndexOf("/") + 1);
if (result != null) {
hasExternalStoragePublicPicture(name);
saveToSDCard(result, name);
isImage = true;
} else {
isImage = false;
}
}
}
public void saveToSDCard(Bitmap bitmap, String name) {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
Log.v(TAG, "SD Card is available for read and write "
+ mExternalStorageAvailable + mExternalStorageWriteable);
saveFile(bitmap, name);
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Log.v(TAG, "SD Card is available for read "
+ mExternalStorageAvailable);
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
Log.v(TAG, "Please insert a SD Card to save your image "
+ mExternalStorageAvailable + mExternalStorageWriteable);
}
}
private void saveFile(Bitmap bitmap, String name) {
String filename = name;
ContentValues values = new ContentValues();
File sdImageMainDirectory = new File(Environment
.getExternalStorageDirectory(), getResources().getString(
R.string.directory));
sdImageMainDirectory.mkdirs();
File outputFile = new File(sdImageMainDirectory, filename);
values.put(MediaStore.MediaColumns.DATA, outputFile.toString());
values.put(MediaStore.MediaColumns.TITLE, filename);
values.put(MediaStore.MediaColumns.DATE_ADDED, System
.currentTimeMillis());
values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
Uri uri = this.getContentResolver().insert(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
try {
OutputStream outStream = this.getContentResolver()
.openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.PNG, 95, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean hasExternalStoragePublicPicture(String name) {
File sdImageMainDirectory = new File(Environment
.getExternalStorageDirectory(), getResources().getString(
R.string.directory));
File file = new File(sdImageMainDirectory, name);
if (file != null) {
file.delete();
}
return file.exists();
}
and this is how i read it:
String imageInSD = c.getString(5); //"/sdcard/Hanud/image1.jpg"or image2;
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
myImageView=(ImageView)findViewById(R.id.imageview1);
myImageView.setImageURI(null);
myImageView.setImageBitmap(bitmap);
I'm facing a problem: I have a stored data on /data/data/files and o have a menu button "export", when i tap on it the file is well exported in the SDCARD but with 0 as size (no data inside the file).
export.class code:
public Export(Context context,String nom) {
this.context = context;
this.nom=nom;
}
public void transfer(){
try {
File sdCard = Environment.getExternalStorageDirectory();
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
Log.d("Carburant", "Sdcard can read/write !!" );
mExternalStorageAvailable = mExternalStorageWriteable = true;
File dir = new File (sdCard.getAbsolutePath() + "/Carburant/");
dir.mkdirs();
File file = new File(dir, "settings.dat");
//FileOutputStream f = new FileOutputStream(file);
copyfile(nom,file.getAbsolutePath());
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
Log.d("Carburant", "Sdcard only read !!" );
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
}
catch (Exception e) {
Log.d("CARBURANT", e.getMessage());
}
}
private void copyfile(String srFile, String dtFile){
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream 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();
String x=ex.getMessage();
Log.d("Carburant", x);
}
catch(IOException e){
Toast.makeText(context, "Echec", Toast.LENGTH_SHORT).show();
}
}
}
Code where to write to the file:
if (data != "" ) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
myIO.WriteSettings(context,fileDir+fileName, data);
data = "";
WriteSettings method:
public static void WriteSettings(Context context, String nom, String data) {
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try {
fOut = context.openFileOutput(nom, Context.MODE_APPEND);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
osw.close();
fOut.close();
Menu export button:
case R.id.export:
String mainDirPath = this.getFilesDir() + File.separator + "settings.dat";
FileOutputStream fos;
try {
fos = this.openFileOutput("settings.dat", Context.MODE_PRIVATE);
Export myExport = new Export(this,mainDirPath);
myExport.transfer();
} catch (FileNotFoundException e) {
Log.d("Carburant","File Not found");
}catch(IOException e){
Log.d("Carburant","ERROR");
}
return true;
Edited menu export button:
case R.id.exporter:
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
String mainDirPath = this.getFilesDir() + File.separator + fileDir+fileName;
Log.d("Carburant",mainDirPath);
FileOutputStream fos;
try {
fos = this.openFileOutput(fileDir+fileName, Context.MODE_PRIVATE);
Export myExport = new Export(this,mainDirPath);
myExport.transfer();
} catch (FileNotFoundException e) {
Log.d("Carburant","File Not found");
}catch(IOException e){
Log.d("Carburant","ERROR");
}
return true;
Thank you for your help.
maybe have a look at this line "while ((len = in.read(buf)) > 0)"
byte[] buffer = new byte[1024];
int len = in.read(buffer);
while (len != -1) {
out.write(buffer, 0, len);
len = in.read(buffer);
}
In your Menu export button code above you are doing this...
case R.id.export:
String mainDirPath = this.getFilesDir() + File.separator + "settings.dat";
This means mainDirPath will be something like...
/data/data/<package name>/files/settings.dat
The problem is when you write to the file...
String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
myIO.WriteSettings(context,fileDir+fileName, data);
This means that when you create mainDirPath it should be something like...
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String mainDirPath = this.getFilesDir() + File.separator + preferences.getString("login", "") + "." + preferences.getString("marque", "") + "." + "settings.dat";
The confusion is this line...
fos = this.openFileOutput("settings.dat", Context.MODE_PRIVATE);
...which creates an empty file called...
/data/data/<package name>/files/settings.dat
That file is the one you are copying instead of the one called login.marque.settings.dat.
Does that make sense?
I'd suggest single-stepping through this in the debugger, and seeing what happens inside the copyfile method. Does the src file 'nom' exist, with non-zero size?
Best wishes,
Phil Lello