Cannot save a file to devices Download folder using getExternalFilesDir() - java

I spent hours trying to solve this problem and got nowhere!
I am trying to save magnetometer data to csv file.
I am using FastCSV library.
Initially I used getExternalStoragePublicDirectory to save the file to the Downloads directory in my phone.
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "magnetic" + value + ".csv");
But since its been depreciated I used
File file = new File(context.getExternalFilesDir(DIRECTORY_DOWNLOADS), "magnetic" + value + ".csv");
I get a toast message saying that my file is saved in the memory but I cannot find them when I look for them in my phone's Download location. Instead they are stored in my phone's Android directory.
Here's the code:
private int REQUEST_CODE = 1;
private View.OnClickListener listenerStopButton = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(recording == true)
{
recording = false;
counter = 0;
String value = fileIDEdit.getText().toString();
stateText.setText("Recording Stopped");
stateText.setTextColor(Color.parseColor("#0000FF"));
if (storagePermitted((Activity) context)){
csvWriter = new CsvWriter();
File file = new File(context.getExternalFilesDir(DIRECTORY_DOWNLOADS), "magnetic" + value + ".csv");
//File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "magnetic" + value + ".csv");
try {
csvWriter.write(file, StandardCharsets.UTF_8, magneticData);
Toast.makeText(MainActivity.this, "File is recorded in memory.", Toast.LENGTH_LONG).show();
} catch (IOException io) {
Log.d("Error", io.getLocalizedMessage());
}
}
}
else{
Toast.makeText(MainActivity.this, "Nothing to save. Recording was not started.", Toast.LENGTH_LONG).show();
}
}
};
#Override
protected void onResume(){
super.onResume();
sensorManager.registerListener(this, magnetic, SensorManager.SENSOR_DELAY_FASTEST);
}
This code checks for permission:
private static boolean storagePermitted(Activity activity){
// Check read write permission
Boolean readPermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
Boolean writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
if (readPermission && writePermission){
return true;
}
ActivityCompat.requestPermissions(activity, new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUESTCODE_STORAGE_PERMISSION);
return false;
}
Please let me know how can I use getExternalFilesDir() to save the file to Downloads directory!

Related

Permission to save text in storage android not working

I have build sample app for user to write some text and save it on internal storage.
Format TXT or pdf. I have tried on android Kitkat sdk 19 and It's working fine but when I tried on the latest android 9 I got denied could not save the text in internal storage
Edit : I wonder If have to manually create My files folder
What I'm I missing ?
AndroidManifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSaveTextBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
mText = mResultEt.getText().toString().trim();
if(mText.isEmpty()){
Toast.makeText(MainActivity.this, "write text First...", Toast.LENGTH_SHORT).show();
}
else{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED){
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permissions,WRITE_EXTERNAL_STORAGE_CODE);
}else {
saveToTxtFile(mText);
}
}else {
saveToTxtFile(mText);
}
}
}
});
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode){
case WRITE_EXTERNAL_STORAGE_CODE: {
if (grantResults.length > 0 && grantResults[0]
== PackageManager.PERMISSION_GRANTED) {
//permission granted save data
saveToTxtFile(mText);
} else {
//permission denied
Toast.makeText(this, "Storage Permission required to store", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
private void saveToTxtFile(String mText){
//get current time for file name
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(System.currentTimeMillis());
try{
//path tp storage
File path = Environment.getExternalStorageDirectory();
//create folder named "My file"
File dir = new File( path + "/My Files/");
dir.mkdirs();
//file name
String fileName = "MyFile_" + timestamp + ".txt";
File file = new File (dir, fileName);
//used to store characater in file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(mText);
bw.close();
//show file name and path where file saved
Toast.makeText(this, fileName+"is saved to\n" +dir, Toast.LENGTH_SHORT).show();
}catch (Exception e){
//if anything goes wrong
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
As suggested both Shashanth and kashyap answers.
This is just a workaround but it works.
I have added
android:requestLegacyExternalStorage="true"
to my <application> element in the AndroidManifest.xml file.
And changed the line
File path = Environment.getExternalStorageDirectory();
to
File path = new File(getExternalFilesDir(null).getAbsolutePath());
In your MainActivity you should check if permission is granted or not, then use != operator.
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {}
this line should be-
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){}

How do I Store a String to a file in android

I am trying to store a String to a file which should be stored on the device
I have the following code that stores the String into a variable called exportstring:
final JSONArray jsonArray1 = jsonArray;
JSONObject export = jsonArray1.getJSONObject(index);
String exportString = export.toString();
writeToFile(exportString);
I then have a function called writeToFile
private void writeToFile(String content) {
try {
File file = new File(Environment.getExternalStorageDirectory() + "/test.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(file);
writer.append(content);
writer.flush();
writer.close();
} catch (IOException e) {
}
}
when I run it, I get a message coming up saying "The file was saved"
but when I open a file manager, I do not see it anywhere.
where is the file saved to?
or have I done something wrong here?
To store the file in Downloads folder, change:
File file = new File(Environment.getExternalStorageDirectory() + "/test.txt");
to:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/test.txt");
UPDATE: Before trying to save the file you should make sure you have the required permissions, in this case the permission to write to storage. Below is a short example of how to do that:
protected boolean checkPermission() {
int result1 = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result1 == PackageManager.PERMISSION_GRANTED ) {
return true;
} else {
return false;
}
}
protected void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(this, "Write External Storage permission allows us to do store files. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 100:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("permissionCheck", "Permission Granted, do what you want to do when user grants the permission here!!!");
} else {
Log.d("permissionCheck", "Permission Denied, do what you want to do when user denies the permission here...");
}
break;
}
}
Include the code above to your activity (or any activity before the one you currently are at) and before trying to store the string to the file do the following:
if(!checkPermission()){
Log.d("permissionCheck", "Need to get the permissions");
requestPermission();
}else{
Log.d("permissionCheck", "Already have the permissions");
}
To see the logs in logcat :

Android - Can't display image from filepath / Can't save image to device

I'm making a simple notebook app, in which a note can have an image attached to it. I can take a photo just fine, and after the photo has been taken, it is correctly displayed in an imageview inside the note. After the note has been saved, the path to the photo is saved in a database. The path is correctly saved. Next, when the user opens the note, the path is correctly found in the database -- however, I am not able to load it into the imageview. Rather, the imageview turns white. As I manually take a look into the folder where the image is supposed to be saved, I see that the image isn't being saved to the phone or SD card at all, and I have no idea how to save it. I've been looking all over the internet and StackOverflow, but haven't found a solution.
public void onIcTakePhotoClick(View icon) {
File imageFile = null;
Intent takePictureIntent;
try {
imageFile = ImageHelper.createImageFile(this);
} catch (IOException e) {
e.printStackTrace();
}
if (imageFile != null) {
mImagePath = imageFile.getAbsolutePath();
//mImagePath = ImageHelper.getImagePath(imageFile);
takePictureIntent = ImageHelper.dispatchTakePictureIntent(this, imageFile);
startActivityForResult(takePictureIntent, ImageHelper.REQUEST_IMAGE_CAPTURE);
} else {
// error message
}
}
All of the above (creating a file, a path, and an intent) work well:
public static File createImageFile(Context context) throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
public static Intent dispatchTakePictureIntent(Context context, File photoFile) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
// Continue only if the File was successfully created
Uri photoURI = FileProvider.getUriForFile(context,
"com.bergdahl.notebook.fileprovider",
photoFile);
//takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
return takePictureIntent;
}
return null;
}
The filepath ends up looking like this: "/storage/emulated/0/Android/data/com.myname.notebook/files/Pictures/JPEG_20160824_213643_135920553.jpg". Next I get the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ImageHelper.REQUEST_IMAGE_CAPTURE: {
if (resultCode == RESULT_OK) {
ImageHelper.handleCameraPhoto(this, imageView, mImagePath);
}
break;
}
}
}
In which I use methods created by the Android team (https://developer.android.com/training/camera/photobasics.html):
public static void handleCameraPhoto(Activity context, ImageView imageView, String imagePath) {
if (imagePath != null) {
ImageHelper.setPic(imagePath, imageView);
ImageHelper.galleryAddPic(context, imagePath);
}
}
public static void setPic(String imagePath, ImageView imageView) {
/* There isn't enough memory to open up more than a couple camera photos */
/* So pre-scale the target bitmap into which the file is decoded */
/* Get the size of the ImageView */
int targetW = imageView.getWidth();
int targetH = imageView.getHeight();
/* Get the size of the image */
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
/* Figure out which way needs to be reduced less */
int scaleFactor = 1;
if ((targetW > 0) || (targetH > 0)) {
scaleFactor = Math.min(photoW/targetW, photoH/targetH);
}
/* Set bitmap options to scale the image decode target */
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
/* Decode the JPEG file into a Bitmap */
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, bmOptions);
/* Associate the Bitmap to the ImageView */
imageView.setImageBitmap(bitmap);
imageView.setVisibility(View.VISIBLE);
}
public static void galleryAddPic(Activity context, String imagePath) {
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
File f = new File(imagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
And finally, the code for opening up the note later:
if (mNote.getFilePath() != null) {
mImagePath = mNote.getFilePath();
//ImageHelper.handleCameraPhoto(this, imageView, mNote.getFilePath());
//File file = new File(mImagePath);
//imageView.setImageDrawable(Drawable.createFromPath(mImagePath));
//Uri uri = Uri.parse("file:" + mNote.getFilePath());
//imageView.setImageURI(uri);
}
In the manifest, I've specified the permission for writing to external storage. So, to reiterate, I need help figuring out how to save the image that I've taken onto the device, so that I can later load it into an imageview next time the user opens the note. Thank you in advance for the help!
EDIT: I tried the image.createNewFile()-method, but the outcome is sadly the same. The boolean test is true.
public static File createImageFile(Context context) throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
/*
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
*/
File image = new File(storageDir, imageFileName + ".jpg");
if (!image.exists()) {
try {
boolean test = image.createNewFile();
Log.d("createImageFile", test + "");
} catch (IOException e) {
e.printStackTrace();
}
}
return image;
}
EDIT again: The image now correctly shows up on an emulator, but it doesn't work on my actual device. The emulator runs 6.0, my device 6.0.1. My device has granted permissions and has external storage. I've been trying to change the directory, but to no success.
Try to replace
mImagePath = imageFile.getAbsolutePath();
with
mImagePath = "file:" + imageFile.getAbsolutePath();
With Android 6 you won't have the read/write permission after installing even if you have declared it in your manifest. You have to ask for it first at runtime:
https://developer.android.com/training/permissions/requesting.html
For testing purposes you can give your app the permissions in the app settings menu without implementing the query.
add the Permission runtime..
if (Build.VERSION.SDK_INT >= 23)
{
if (checkPermission())
{
// Code for above or equal 23 API Oriented Device
// Create a common Method for both
///take camera caputure code
} else {
requestPermission();
}
}
else
{
// Code for Below 23 API Oriented Device
// Create a common Method for both
}
Now adding checkPermission() and requestPermission()
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(Your_Activity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(Your_Activity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(Your_Activity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(Your_Activity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("value", "Permission Granted, Now you can use local drive .");
} else {
Log.e("value", "Permission Denied, You cannot use local drive .");
}
break;
}
}

Download Manager Unable to Resume Download in case of Internet Disconnection and System Reboot

I have created a simple application which is supposed to download large zip files. After some R&D I came to the conclusion that I have to use Download Manager to achieve this. I want the download to resume automatically if the device is restarted or in case of unstable internet connectivity. Right now, the code is able to download large files as expected, but in case of internet connectivity fluctuations or system restart, it stops downloading.
The activity:
public class MainActivity extends ActionBarActivity {
String Download_path = "http://wickedbrains.com/map/mumbai.zip";
String Download_ID = "DOWNLOAD_ID";
SharedPreferences preferenceManager;
DownloadManager downloadManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferenceManager = PreferenceManager.getDefaultSharedPreferences(this);
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
Button btnDownload = (Button)findViewById(R.id.download);
btnDownload.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// Locate storage location
String filepath = "";
File folder = new File(
Environment.getExternalStorageDirectory() + "/osmdroid");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
// Do something on success
filepath = Environment.getExternalStorageDirectory()
.getPath() + "/osmdroid";
// Deleting if zip file exists
File folder2 = Environment.getExternalStorageDirectory();
String fileName = folder2.getPath() + "/osmdroid/mumbai.zip";
File myFile = new File(fileName);
if(myFile.exists())
myFile.delete();
}
//Starting download manager to download file
Uri Download_Uri = Uri.parse(Download_path);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
long download_id = downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Test")
.setDescription("Map Download")
.setDestinationInExternalPublicDir("/osmdroid","mumbai.zip"));
// long download_id = downloadManager.enqueue(request);
//Save the download id
Editor PrefEdit = preferenceManager.edit();
PrefEdit.putLong(Download_ID, download_id);
PrefEdit.commit();
}});
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(downloadReceiver, intentFilter);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(downloadReceiver);
}
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(preferenceManager.getLong(Download_ID, 0));
Cursor cursor = downloadManager.query(query);
if(cursor.moveToFirst()){
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
if(status == DownloadManager.STATUS_SUCCESSFUL){
//Retrieve the saved download id
long downloadID = preferenceManager.getLong(Download_ID, 0);
ParcelFileDescriptor file;
try {
file = downloadManager.openDownloadedFile(downloadID);
Toast.makeText(MainActivity.this,
"File Downloaded: " + file.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
}
}else if(status == DownloadManager.STATUS_FAILED){
Toast.makeText(MainActivity.this,
"FAILED!\n" + "reason of " + reason,
Toast.LENGTH_LONG).show();
}else if(status == DownloadManager.STATUS_PAUSED){
Toast.makeText(MainActivity.this,
"PAUSED!\n" + "reason of " + reason,
Toast.LENGTH_LONG).show();
}else if(status == DownloadManager.STATUS_PENDING){
Toast.makeText(MainActivity.this,
"PENDING!",
Toast.LENGTH_LONG).show();
}else if(status == DownloadManager.STATUS_RUNNING){
Toast.makeText(MainActivity.this,
"RUNNING!",
Toast.LENGTH_LONG).show();
}
}
}
};
}
Where am I going wrong? What should I do to enable the resume capability of the download?
Quoting from docs,
The download manager will conduct the download in the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system reboots.
I guess Download Manager, by default takes cares of retries.
If you are having issues you can use DownloadManager.Query class and query for COLUMN_STATUS and COLUMN_REASON to get the download status
Edit:
Starting a download
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request( YOUR_DOWNLOAD_URL );
long enqueue = dm.enqueue(request);
enqueue is more like a download reqeust id. You can use that enqueue to fetch the download progress/status
Querying the download Status
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int downloadStatus = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (DownloadManager.STATUS_SUCCESSFUL == downloadStatus) {
// download succeded
} else if (DownloadManager.STATUS_FAILED == downloadStatus){
String failedReason = c.getString(c.getColumnIndex(DownloadManager.COLUMN_REASON));
// handle failures
}
}
Haven't tested the code myself. But it should work.
I confirm that this problem still exists in 2020, when testing in an emulator and having WiFi enabled, this error consistently appears (even with Android 10).
Switching off WiFi in the emulator seems to solve the problem.
Try to get the reason for the failed download.
e.g does it work on network switch wifi->data
(If your error reason is 1008- there seems to be a reported bug here
https://code.google.com/p/android/issues/detail?id=18462,
further:
http://papaya-backend.net/2013/04/12/why-http-etag-header-may-cause-your-downloading-apps-on-android-failed/)

Code not writing database to sdcard

I'm using the following code to export a copy of my database to my sdcard.
public class AgUtility extends AgActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.utility);
try {
backupDatabase(getBaseContext());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void backupDatabase(Context context) throws IOException {
// Open your local db as the input stream
String inFileName = "data/data/com.agmanagement.todaysstudent/databases/todaysstudent.db";
Toast.makeText(context, "FileName Is "+ inFileName, Toast.LENGTH_LONG).show();
Log.i("The File In Is ", inFileName);
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
File outputDirectory = new File(
Environment.getExternalStorageDirectory() + "/student/");
outputDirectory.mkdir();
Log.d("MAKE DIR", dbFile.mkdir() + "");
String backupFileName = "/TodaysStudentTest.db3";
String outFileName = outputDirectory + backupFileName;
Toast.makeText(context, "Database backup names is " + outFileName , Toast.LENGTH_LONG)
.show();
// Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
Toast.makeText(context, "Database backup complete", Toast.LENGTH_LONG)
.show();
}
}
The code seems to work properly, in that I don't get any errors the first Toast shows the correct database name, the second toast shows the output directory should be mnt/sdcard/student and the third shows the final target should be mnt/sdcard/student/TodaysStudentTest.db3
After that Toast fades, nothing, the final Toast never appears.
In my manifest I have
I am testing this on a Samsung Tablet and not on the emulator, i've also run it on a DroidX with the same result, no errors, but no folder is created.
Any ideas on what I'm doing wrong?
TIA
The permissions I'm using are
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.premission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
I get the same results when running in the emulator - watching with the DDMS - Logcat show MAKE DIR fails.
I've tested for state with this
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
Toast.makeText(getBaseContext(), "We Can Read And Write To The SDCARD", Toast.LENGTH_LONG).show();
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Toast.makeText(getBaseContext(), "We Can Read The SDCARD", 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(getBaseContext(), "We Can't read or write", Toast.LENGTH_LONG).show();
}
And it shows I'm supposed to be able to read and write, so there's something wrong with how I'm writing. I added this to also text
boolean success = false;
if(!outputDirectory.exists()){
Toast.makeText(getBaseContext(), "Folder Doesn't Exist ", Toast.LENGTH_LONG)
.show();
success = outputDirectory.mkdirs();
}
if (!success){
Toast.makeText(getBaseContext(), "Folder Not Created ", Toast.LENGTH_LONG)
.show();
}
else{
Toast.makeText(getBaseContext(), "Folder Created ", Toast.LENGTH_LONG)
.show();
}
Results are folder does not exist, and then mkdirs() fails.
REWRITE
Here is a different approach to coping a database file, without using SQL itself or a looping buffer.
NOTE: This isn't actually copied to the sdcard, the backup is stored in the original databases folder (which I like because you do not need WRITE_EXTERNAL_STORAGE permission).
public class FileIO extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBHelper db = new DBHelper(this);
try {
copyFile();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
Log.i("Main", "Complete");
db.close();
finish();
}
}
public void copyFile() throws IOException {
File data = Environment.getDataDirectory();
String state = Environment.getExternalStorageState();
/* Create file first
FileOutputStream created = openFileOutput("copyFile.db", MODE_WORLD_READABLE);
created.close();
*/
String currentDBPath = "/data/<your_path>/databases/data.db";
String backupDBPath = "/data/<your_path>/databases/copyByFile.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(data, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
else
Log.i("Main", "Current db does not exist");
}
}
Try to manually create file before trying to write to it.
please make sure you have already created folder named "student" as you are using mkdir(). it will create directory by abstract path name..so if folder "student" does not exist it wont create new folder.. or try instead mkdirs(). it will created parent folder if necessary.
Important to remember to check spelling. uses-permission was mis-spelled as uses-premission, I had read the code so many times I read it as I wanted it to be. valuable lesson, walk away and take a break.

Categories