Code not writing database to sdcard - java

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.

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){}

Create File/Database from SD card

I'm beginner from Android Studio
i can create also a file from Phone Storage but i need is to How to create a file from SD card. im using virtual device or i9000S.
actually i'm using:
Android Jellybean
API Level: 18
Android Version: 4.3
if i use this File myFile = new File("/sdcard/sample.txt");, it works.
when i use this File myFile = new File("/sdcard1/sample.txt");, it does'nt work. it gives me an error like Error: open failed: ENOENT (No such file or directory).
MainActivity.java:
final String NEW_FOLDER_NAME = "TestFolder";
testPath(new File(Environment.getExternalStorageDirectory(), NEW_FOLDER_NAME));
testPath(new File("/storage/emulated/0/", NEW_FOLDER_NAME));
testPath(new File("/storage/emulated/1/", NEW_FOLDER_NAME));
testPath(new File("/storage/sdcard0/Download/", NEW_FOLDER_NAME));
testPath(new File("/storage/sdcard1", NEW_FOLDER_NAME));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
String E1 = System.getenv("EXTERNAL_STORAGE");
File F1 = new File(E1, NEW_FOLDER_NAME);
String E2 = System.getenv("SECONDARY_STORAGE");
File F2 = new File(E2, NEW_FOLDER_NAME);
testPath(new File("/storage/sdcard1", NEW_FOLDER_NAME));
testPath(F1);
testPath(F2);
File myFile = new File("/sdcard1/sample.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(e1.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(Main1Activity.this, "Save to" + getFilesDir() + ">" + NEW_FOLDER_NAME, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
private void testPath(File path) {
String TAG = "Debug.MainActivity.java";
String FOLDER_CREATION_SUCCESS = " mkdir() success: ";
boolean success;
if (path.exists()) {
// already created
success = true;
} else {
success = path.mkdir();
}
Log.d(TAG, path.getAbsolutePath() + FOLDER_CREATION_SUCCESS + success);
path.delete();
}
edit:
i already add from manifest file:
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
From official document -
This snippet will create file on your internal storage. WRITE_EXTERNAL_STORAGE permission is not required.
// Create new file and write
File file = new File(context.getFilesDir(), filename);
String filename = "yourFileName.txt";
String fileContents = "Insert your data here.";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(fileContents.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
// Opening a file
File directory = context.getFilesDir();
File file = new File(directory, filename);
// Here you can re-write or edit your file
Please take a look on this -
Read/Write internal storage
For obtaining sdcard root path you should use
Environment.getExternalStoragexxx

Android not create a folder in gallery

I'm trying to save an image on JPG format on a specific folder from my gallery. But my code is not creating a directory, whenever i create a Toast it return for me /storage/emulated/0/DCIM/MyFodler,but when will i open the gallery, this foder not exist. I'm building the application direct of my devide with Android Marshmallow 6.0.
Code to create Bitmap:
private Bitmap getToBitmap(ImageView view, int Width, int Heigth){
Bitmap bitmap = Bitmap.createBitmap(Width,Heigth, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
Code to try save the image on gallery:
private void TrySaveMediaStore(){
String path = Environment.getExternalStorageDirectory().toString();
OutputStream FileOut = null;
File file = new File(path,"DCIM/MyFolder");
file.mkdirs();
Toast.makeText(getApplicationContext(),file.getAbsolutePath(),Toast.LENGTH_SHORT).show();
try{
FileOut = new FileOutputStream(file);
FileOut.flush();
FileOut.close();
Bitmap bitmap = getToBitmap(img,img.getMaxWidth(),img.getMaxHeight());
bitmap.compress(Bitmap.CompressFormat.JPEG,100,FileOut);
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
Toast.makeText(this,file.getAbsolutePath(),Toast.LENGTH_SHORT).show();
}catch (FileNotFoundException e){
return;
}catch (IOException e){
e.printStackTrace();
}
}
Androidmanifest permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
DCIM/MyFolder is a directory. You create this as a directory using mkdirs().
You cannot then try using DCIM/MyFolder as a filename for saving a JPEG. You need to create a file inside the directory.
So, instead of:
FileOut = new FileOutputStream(file);
use something like:
File theActualImageFile=new File(file, "something.jpeg");
FileOut = new FileOutputStream(theActualImageFile);
Also:
You need to deal with runtime permissions, if your targetSdkVersion is 23 or higher
A gallery app will see neither the directory nor the file, until you tell the MediaStore to index the newly-created JPEG
i think a had the same problem, actually the image do insert just fine in the memory, but when i tried to watch it didn't show as i expected, i solved it refreshing the gallery with the scanner class, used this code:
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
See this link for more info: How can I refresh the Gallery after I inserted an Image in android?
You may use the below code for asking runtime storage permission:
final int MyVersion = Build.VERSION.SDK_INT;
if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
if (!checkIfAlreadyhavePermission()) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
TrySaveMediaStore() ;
}
checkIfAlreadyhavePermission() method:
private boolean checkIfAlreadyhavePermission() {
int result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED;
}
Add onRequestPermission():
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
TrySaveMediaStore();
} else {
Toast.makeText(this, "Please give your permission.", Toast.LENGTH_LONG).show();
}
break;
}
}
}
After creating the file scan MediaStore:
public void scanFile(Context c, File file, String mimeType) {
MediaScannerConnection
.scanFile(c, new String[] {file.getAbsolutePath()},
new String[] {mimeType}, null);
}
Yes, the problem is the media scanner. Yo can simply check the file using a terminal (download the app if you don't have it) and go manually to the directory. I had the same problem, but at least I know the file is there.

Android: how to write a file locally and save it to the Downloads

How do you write files locally and save them to the Downloads App in Android?
Android version: Nougut
The file is not showing in Downloads though. Here's my code:
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "foo.txt");
try {
String exampleString = "bar\nfoo";
InputStream is = new ByteArrayInputStream(exampleString.getBytes(Charset.forName("UTF-8")));
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
throw new AssertionError(e.toString());
}
MediaScannerConnection.scanFile(
getContext(),
new String[]{file.getAbsolutePath()},
null,
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String s, Uri uri) {
Log.d(TAG, "String: "+ s);
Log.d(TAG, "Uri: "+ uri );
}
});
It is logging this in onScanCompleted so it seems like the file should show in Downloads but it doesn't.
D/SignupFragment: String: /storage/emulated/0/Download/foo.txt
D/SignupFragment: Uri: content://media/external/file/84691
I have read the Android docs on saving files
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The standard AOSP Downloads app only shows what DownloadManager downloaded. It does not show files placed in the Downloads/ directory by other means.

Unable to write a txt file to SD card on Android KitKat

I keep trying to write to the SD card in my java code but whenever i check my card the folder and file isnt there; i know that for KitKat you have to use .getExternalFilesDir but so far nothing is working.
my current code:
String DataIn = PhoneNumber + "," + dataLong + "," + dataLat;
File storageDirectory = new File (this.getExternalFilesDir(null), "location.txt");
if(!storageDirectory.exists()) {
try {
storageDirectory.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
String Directory = storageDirectory.toString();
try
{
FileOutputStream fout = new FileOutputStream(storageDirectory, true);
OutputStreamWriter myoutwriter = new OutputStreamWriter(fout);
myoutwriter.write(DataIn);
myoutwriter.close();
Toast.makeText(getBaseContext(),"Saved", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
Try this:
File storageDirectory = new File(Environment.getExternalStorageDirectory(), "location.txt");
Make sure you have this permission in your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Categories