Friday, February 28, 2014

android in the database content is written to an external txt file


database = SQLiteDatabase.openOrCreateDatabase (DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
Cursor c = database.rawQuery ("select * from t_firstcheckinfo", null);
if (c.moveToFirst ())
{
while (! c.isAfterLast ())
{
int id = c.getInt (0);
. ((Declare) getApplication ()) setId (id);
String barcode = c.getString (1);
. ((Declare) getApplication ()) setBarcode (barcode);
int number = c.getInt (2);
. ((Declare) getApplication ()) setNumber (number);
String firstemployee = c.getString (3);
. ((Declare) getApplication ()) setUser (firstemployee);
String shopname = c.getString (4);
. ((Declare) getApplication ()) setShopname (shopname);
String shelfno = c.getString (5);
. ((Declare) getApplication ()) setShelfno (shelfno);
String firsttime = c.getString (6);
. ((Declare) getApplication ()) setTime (firsttime);
String firstposition = c.getString (7);
. ((Declare) getApplication ()) setPosition (firstposition);
String goodsname = c.getString (8);
. ((Declare) getApplication ()) setGoodsname (goodsname);
c.moveToNext ();

}
}
Intent intent = new Intent (DataexportActivity.this, WritetxtActivity.class);
startActivity (intent);


}

});


Then WritetxtActivity.class code

public class WritetxtActivity extends Activity {
private SQLiteDatabase database;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
try {
database = SQLiteDatabase.openOrCreateDatabase (DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
Cursor c1 = database.rawQuery ("select * from t_firstcheckinfo", null);
FileOutputStream fos = new FileOutputStream ("D: \ \ checkinfo.txt");
OutputStreamWriter osw = new OutputStreamWriter (fos);
BufferedWriter bf = new BufferedWriter (osw);
for (int i = 0; i {
bf.newLine () ;/ / first wrap
int id = ((Declare) getApplicationContext ()) getId ();.
bf.write (id + "\ t");
bf.flush ();
String goodsname = ((Declare) getApplicationContext ()) getGoodsname ();.
bf.write (goodsname + "\ t");
bf.flush ();
. String barcode = ((Declare) getApplicationContext ()) getBarcode ();
bf.write (barcode + "\ t");
bf.flush ();
. int number = ((Declare) getApplicationContext ()) getNumber ();
bf.write (number + "\ t");
bf.flush ();
String employee = ((Declare) getApplicationContext ()) getUser ();.
bf.write (employee + "\ t");
bf.flush ();
String shopname = ((Declare) getApplicationContext ()) getShopname ();.
bf.write (shopname + "\ t");
bf.flush ();
String shelfno = ((Declare) getApplicationContext ()) getShelfno ();.
bf.write (shelfno + "\ t");
bf.flush ();
String time = ((Declare) getApplicationContext ()) getTime ();.
bf.write (time + "\ t");
bf.flush ();
String position = ((Declare) getApplicationContext ()) getPosition ();.
bf.write (position + "\ t");
bf.flush ();
bf.newLine ();
bf.flush ();


}
bf.close ();
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}

}
}
Now the problem is whenever I debug, after entering WritetxtActivity, do not go for the cycle, the program went directly to where the catch, logcat then prompts the following information:

I do not know how to solve ah, beg solutions.<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
The problem is clear, but can not find the files in the D drive, why do you want to write files to the D drive?
Reply:
No permission, Manifest, adding a
  

Also note that the txt file path is correct.
Reply:
cited a floor q445697127 reply:
question is very clear, but could not find the files in the D drive, why do you want to write files to the D drive?

I am now trying to android read out the contents of the database program, convert txt, and then save the file to a folder inside your computer, in order to facilitate the user to view.
Reply:
reference to the second floor tantahe reply:
no authority, Manifest years plus
  

Also note that the txt file path is correct.

I've added in the Manifest permissions, but the problem still occurs, really do not know what the problem is Kazakhstan. The help of experts to look at Kazakhstan.
Reply:
Your program can run in android file output to the D drive?? You should save the file to SDCard top anti ..
Reply:
reference to the third floor lhx200610930202 reply:
Quote: references to a floor q445697127 reply:

The problem is clear, but can not find the files in the D drive, why do you want to write files to the D drive?

I am now trying to android read out the contents of the database program, convert txt, and then save the file to a folder inside your computer, in order to facilitate the user to view.

When exporting data to a computer in the past I thought derived (not sure I can do that), not all conventional cooking guide to the SD card it?
Reply:
reference to the 6th floor q445697127 reply:
Quote: references to the third floor lhx200610930202 reply:

Quote: references to a floor q445697127 reply:

The problem is clear, but can not find the files in the D drive, why do you want to write files to the D drive?

I am now trying to android read out the contents of the database program, convert txt, and then save the file to a folder inside your computer, in order to facilitate the user to view.

When exporting data to a computer in the past I thought derived (not sure I can do that), not all conventional cooking guide to the SD card it?

I'm also not sure can not do that, my database tables are imported from outside, and then re-generate a new database table in the code, I just want this new database table guide, and to facilitate the user to view content. What if the guide to the sd card, how to achieve it? We look to enlighten
Reply:
http://blog.csdn.net/q445697127/article/details/7519761
This is an example I wrote before, you can probably look
Reply:
reference to the third floor lhx200610930202 reply:
Quote: references to a floor q445697127 reply:

The problem is clear, but can not find the files in the D drive, why do you want to write files to the D drive?

I am now trying to android read out the contents of the database program, convert txt, and then save the file to a folder inside your computer, in order to facilitate the user to view.

Android can not access the computer's hard drive, save it to your sdcard
Reply:
reference to the 8th floor q445697127 reply:
http://blog.csdn.net/q445697127/article/details/7519761
This is an example I wrote before, you can probably look

/ / Convert the content type of the array of bytes byte [] message = txt.getBytes ();
I want to save the data has type int and string, and now my code is like this
try {
database = SQLiteDatabase.openOrCreateDatabase (DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
Cursor c = database.rawQuery ("select * from t_firstcheckinfo", null);



FileOutputStream fos = new FileOutputStream ("/ sdcard / checkinfo.txt");

OutputStreamWriter osw = new OutputStreamWriter (fos);
BufferedWriter bf = new BufferedWriter (osw);


for (int i = 0; i {
bf.newLine () ;/ / first wrap
int id = ((Declare) getApplicationContext ()) getId ();.
bf.write (id + "\ t");
bf.flush ();
String goodsname = ((Declare) getApplicationContext ()) getGoodsname ();.
bf.write (goodsname + "\ t");
bf.flush ();
. String barcode = ((Declare) getApplicationContext ()) getBarcode ();
bf.write (barcode + "\ t");
bf.flush ();
. int number = ((Declare) getApplicationContext ()) getNumber ();
bf.write (number + "\ t");
bf.flush ();
String employee = ((Declare) getApplicationContext ()) getUser ();.
bf.write (employee + "\ t");
bf.flush ();
String shopname = ((Declare) getApplicationContext ()) getShopname ();.
bf.write (shopname + "\ t");
bf.flush ();
String shelfno = ((Declare) getApplicationContext ()) getShelfno ();.
bf.write (shelfno + "\ t");
bf.flush ();
String time = ((Declare) getApplicationContext ()) getTime ();.
bf.write (time + "\ t");
bf.flush ();
String position = ((Declare) getApplicationContext ()) getPosition ();.
bf.write (position + "\ t");
bf.flush ();
bf.newLine ();
bf.flush ();


}
bf.flush ();
bf.close ();


} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}



}
After write code or jump directly to catch at the fundamental intermediate code is not executed, but also added the authority. Really do not know why, anxious dead.
Reply:
sdcard path is wrong, the "/ sdcard / checkinfo.txt" into android.os.Environment.getExternalStorageDirectory () + "/ checkinfo.txt"
Reply:
reference to the 11th floor tantahe reply:
sdcard wrong path, the "/ sdcard / checkinfo.txt" into android.os.Environment.getExternalStorageDirectory () + "/ checkinfo.txt"

This approach also tried, or not, the code runs to FileOutputStream fos = new FileOutputStream (android.os.Environment.getExternalStorageDirectory () + "/ checkinfo.txt"); this one, then there would just jump catch a.
Reply:
You posted the logcat error
Reply:
Is not to add conditions, if the file does not exist, create it
Reply:
pc android how to access the D drive thiophene? suggest you first wrote on the sdcard and then uploaded to the server on the line pc thiophene
Reply:
try {
database = SQLiteDatabase.openOrCreateDatabase (DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
/ / List = new ArrayList ();
Cursor c = database.rawQuery ("select * from t_firstcheckinfo", null);
/ / String fileNameString = "checkinfo.txt";
/ / Determine whether there sd card Environment.getExternalStorageState () to get the current state
sd card/ / File file = new File (Environment
/ /. GetExternalStorageDirectory (),
/ / FileNameString);

boolean sdCardExist = Environment.getExternalStorageState ()
. Equals (android.os.Environment.MEDIA_MOUNTED); / / determine whether there
sd card
if (sdCardExist)
{
String baseDir = Environment.getExternalStorageDirectory () getAbsolutePath ();.
FileOutputStream fos;
fos = openFileOutput (baseDir, MODE_WORLD_READABLE);
String fileName = "checkinfo.txt";
File f = new File (baseDir + File.separator + fileName);
if (! f.getParentFile (). exists ()) {
. f.getParentFile () mkdirs () ;/ / create a folder, such as: Create / TXT folder f disk / testTXT / two folders.
}
if (! f.exists ()) {
f.createNewFile () ;/ / create a txt file as: testData.txt file
}
FileWriter fileWriter = new FileWriter (f, true);
BufferedWriter bf = new BufferedWriter (fileWriter);
for (int i = 0; i {
bf.newLine ();
int id = ((Declare) getApplicationContext ()) getId ();.
bf.write (id + "\ t");
bf.flush ();
String goodsname = ((Declare) getApplicationContext ()) getGoodsname ();.
/ / Byte [] goodsname1 = goodsname.getBytes ();
bf.write (goodsname + "\ t");
bf.flush ();
. String barcode = ((Declare) getApplicationContext ()) getBarcode ();
bf.write (barcode + "\ t");
bf.flush ();
. int number = ((Declare) getApplicationContext ()) getNumber ();
bf.write (number + "\ t");
bf.flush ();
String employee = ((Declare) getApplicationContext ()) getUser ();.
bf.write (employee + "\ t");
bf.flush ();
String shopname = ((Declare) getApplicationContext ()) getShopname ();.
bf.write (shopname + "\ t");
bf.flush ();
String shelfno = ((Declare) getApplicationContext ()) getShelfno ();.
bf.write (shelfno + "\ t");
bf.flush ();
String time = ((Declare) getApplicationContext ()) getTime ();.
bf.write (time + "\ t");
bf.flush ();
String position = ((Declare) getApplicationContext ()) getPosition ();.
bf.write (position + "\ t");
bf.flush ();
bf.newLine ();
bf.flush ();
}
bf.flush ();
bf.close ();
}

} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
I now put into this program, logcat information

Reply:
Big thank you God's answer above, I already solved the problem, first my right code stickers, encountered the same problem for future friends to learn together.
Reply:
The correct code is try {
database = SQLiteDatabase.openOrCreateDatabase (DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
/ / List = new ArrayList ();
Cursor c = database.rawQuery ("select * from t_firstcheckinfo", null);
/ / String fileNameString = "checkinfo.txt";
/ / Determine whether there sd card Environment.getExternalStorageState () to get the current state
sd card/ / File file = new File (Environment
/ /. GetExternalStorageDirectory (),
/ / FileNameString);

boolean sdCardExist = Environment.getExternalStorageState ()
. Equals (android.os.Environment.MEDIA_MOUNTED); / / determine whether there
sd card
if (sdCardExist)
{
String baseDir = Environment.getExternalStorageDirectory (). GetAbsolutePath () ;/ / get SDCard directory
File f = new File (baseDir + "/ /" + "firstcheckinfo.txt");
if (! f.getParentFile (). exists ()) {
. f.getParentFile () mkdirs () ;/ / create a folder, such as: Create / TXT folder f disk / testTXT / two folders.
}
if (! f.exists ()) {
f.createNewFile () ;/ / create a txt file as: testData.txt file
}
FileWriter fileWriter = new FileWriter (f, true);
BufferedWriter bf = new BufferedWriter (fileWriter);
if (c.moveToFirst ())
{
while (! c.isAfterLast ())
{

int id = c.getInt (0);

bf.write (id + "\ t");
bf.flush ();
String goodsname = c.getString (8);
bf.write (goodsname + "\ t");
bf.flush ();
String barcode = c.getString (1);

int number = c.getInt (2);
bf.write (barcode + "\ t");
bf.flush ();

String firstemployee = c.getString (3);
bf.write (firstemployee + "\ t");
bf.flush ();

String shopname = c.getString (4);
bf.write (shopname + "\ t");
bf.flush ();

String shelfno = c.getString (5);
bf.write (shelfno + "\ t");
bf.flush ();

String firsttime = c.getString (6);
bf.write (firsttime + "\ t");
bf.flush ();

String firstposition = c.getString (7);
bf.write (firstposition + "\ t");
bf.flush ();

bf.newLine ();
bf.flush ();
/ / System.out.println (id + ":" + goodsname + ":" + barcode + ":" + number + ":" + firstemployee + ":" + shopname + ":" + shelfno + ":" + firsttime + ":" + firstposition + " : ");
c.moveToNext ();

}
}

; showDialog ("inventory files are stored in / mnt / sdcard in it!")}
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}

No comments:

Post a Comment