Я пытаюсь экспортировать данные базы данных Sqlite и сохранить их в памяти телефона
Метод экспорта
public void exportNewReadingData()
{
db = new DatabaseHelper(getApplicationContext());
File exportDir =new File(String.valueOf(Environment.getExternalStorageDirectory()));
if(!exportDir.exists())
{
exportDir.mkdirs();
}
File ReadingFile = new File(exportDir, "'" + this.route + "'U.txt");
try
{
ReadingFile.createNewFile();
CSVWriter csvwriter = new CSVWriter(new FileWriter(String.valueOf(ReadingFile)));
SQLiteDatabase sqLiteDatabase = db.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM toPrintBill", null);
while (cursor.moveToNext())
{
String arrstr[] = {"2", cursor.getString(0), padLeft(cursor.getString(33), 10), padLeft(cursor.getString(35),8), padLeft(cursor.getString(5), 3), padLeft(cursor.getString(34), 12), padLeft(cursor.getString(8), 10), padLeft(cursor.getString(7), 4), padLeft(cursor.getString(6), 8)};
csvwriter.writeNext(arrstr);
}
csvwriter.close();
cursor.close();
}
catch (Exception e)
{
Log.e("RecordedBill", e.getMessage(), e);
}
}
Класс CSVWriter
public class CSVWriter
{
private PrintWriter pw;
private char separator;
private char quotechar;
private char escapechar;
private String lineEnd;
/** The character used for escaping quotes. */
public static final char DEFAULT_ESCAPE_CHARACTER = ' ';
/** The default separator to use if none is supplied to the constructor. */
public static final char DEFAULT_SEPARATOR = ',';
/**
* The default quote character to use if none is supplied to the
* constructor.
*/
public static final char DEFAULT_QUOTE_CHARACTER = ' ';
/** The quote constant to use when you wish to suppress all quoting. */
public static final char NO_QUOTE_CHARACTER = '\u0000';
/** The escape constant to use when you wish to suppress all escaping. */
public static final char NO_ESCAPE_CHARACTER = '\u0000';
/** Default line terminator uses platform encoding. */
public static final String DEFAULT_LINE_END = "\n";
/**
* Constructs CSVWriter using a comma for the separator.
*
* @param writer
* the writer to an underlying CSV source.
*/
public CSVWriter(Writer writer) {
this(writer, DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER,
DEFAULT_ESCAPE_CHARACTER, DEFAULT_LINE_END);
}
/**
* Constructs CSVWriter with supplied separator, quote char, escape char and line ending.
*
* @param writer
* the writer to an underlying CSV source.
* @param separator
* the delimiter to use for separating entries
* @param quotechar
* the character to use for quoted elements
* @param escapechar
* the character to use for escaping quotechars or escapechars
* @param lineEnd
* the line feed terminator to use
*/
public CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd) {
this.pw = new PrintWriter(writer);
this.separator = separator;
this.quotechar = quotechar;
this.escapechar = escapechar;
this.lineEnd = lineEnd;
}
/**
* Writes the next line to the file.
*
* @param nextLine
* a string array with each comma-separated element as a separate
* entry.
*/
public void writeNext(String[] nextLine) {
if (nextLine == null)
return;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < nextLine.length; i++) {
if (i != 0) {
sb.append(separator);
}
String nextElement = nextLine[i];
if (nextElement == null)
continue;
if (quotechar != NO_QUOTE_CHARACTER)
sb.append(quotechar);
for (int j = 0; j < nextElement.length(); j++) {
char nextChar = nextElement.charAt(j);
if (escapechar != NO_ESCAPE_CHARACTER && nextChar == quotechar) {
sb.append(escapechar).append(nextChar);
} else if (escapechar != NO_ESCAPE_CHARACTER && nextChar == escapechar) {
sb.append(escapechar).append(nextChar);
} else {
sb.append(nextChar);
}
}
if (quotechar != NO_QUOTE_CHARACTER)
sb.append(quotechar);
}
sb.append(lineEnd);
pw.write(sb.toString());
}
/**
* Flush underlying stream to writer.
*
* @throws IOException if bad things happen
*/
public void flush() throws IOException {
pw.flush();
}
/**
* Close the underlying stream writer flushing any buffered content.
*
* @throws IOException if bad things happen
*
*/
public void close() throws IOException {
pw.flush();
pw.close();
}
}
Манифест
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
E/RecordedBill: Permission denied
java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
at java.io.File.createNewFile(File.java:1005)
at com.vicjames.qiimeterreader.RecordedBill.exportNewReadingData(RecordedBill.java:121)
at com.vicjames.qiimeterreader.RecordedBill$1$1.onClick(RecordedBill.java:64)
at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:202)
at android.app.ActivityThread.main(ActivityThread.java:6962)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
Приложение не обрабатывает sh, но показывает ошибку, что разрешение отклонено, даже если я использую разрешение для запись внешнего хранилища в манифест.
Что мне здесь не хватает?
ReadingFile.createNewFile();
выдает сообщение Результат "File.createNewFile" игнорируется