In fact, it has nothing to do with the upper layer, mainly through C to complete the basic operations of files. Sorry, you don't have enough time to talk about it. Stick it to the key.Code.
Key File Code:
Mainactivity. Java
Package com. Scan. file;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. Toast;
Public class mainactivity extends activity {
Private Static final string tag = "file ";
Private button Doc = NULL;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Initcontrols ();
}
/**
* Initialize the control.
*/
Public void initcontrols (){
Doc = (button) This. findviewbyid (R. Id. btn_do_c );
Doc. setonclicklistener (New mybuttononclicklistener ());
}
/**
* Listening to buttononclick
*
* @ Author lxf
*
*/
Class mybuttononclicklistener implements onclicklistener {
@ Override
Public void onclick (view v ){
Switch (V. GETID ()){
Case R. Id. btn_do_c:
String do_c_result = docmethod ();
Displaymessage (do_c_result );
Break;
}
}
}
/*
* Toast display messages
*/
Private void displaymessage (string MSG ){
Toast. maketext (this, MSG, Toast. length_short). Show ();
}
/**
* Test Method
*
* @ Return
*/
Public native string sayhello ();
/**
* Execute the underlying C method
*
* @ Return
*/
Public native string docmethod ();
/*
* Manned local library files
*/
Static {
System. loadlibrary ("androidjni ");
}
}
File. h
# Include <string. h>
# Include <stdio. h>
# Include "Define. H"
Unsigned short file_open (file_handle ** filehandle, char * Name, unsigned short flag, unsigned short mode );
Unsigned short file_close (file_handle * filehandle );
Unsigned short file_getsize (file_handle * filehandle, unsigned long * filesize );
Unsigned short file_read (file_handle * filehandle, char * Buf, unsigned Long Count, unsigned long * readcount );
Unsigned short file_write (file_handle * filehandle, char * Buf, unsigned Long Count, unsigned long * writecount );
Unsigned short file_seek (file_handle * filehandle, long offset, short origin, unsigned long * seeklen );
Unsigned short file_delete (char * Name );
Unsigned short file_isexist (char * path );
Unsigned short file_create_dir (char * dirname );
Unsigned short file_delete_dir (char * dirname );
Android. mk
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_module: = androidjni
Local_src_files: = androidjni. c file. c syncmlengine. c
Local_ldlibs + =-L $ (sysroot)/usr/lib-llog
Include $ (build_shared_library)
Androidjni. c
# Include <string. h>
# Include <JNI. h>
# Include <Android/log. h>
# Include "syncmlengine. H"
// Test method: sayhello
Jstring java_com_scan_file_mainactivity_sayhello (jnienv * ENV, jobject thiz ){
// Print the information
_ Android_log_print (android_log_info, "jnimsg", "sayhello ");
Return (* env)-> newstringutf (ENV, "hello from JNI! Sayhello ");
}
// Execute the C underlying method
Jstring java_com_scan_file_mainactivity_docmethod (jnienv * ENV, jobject thiz ){
// Print the information
_ Android_log_print (android_log_info, "jnimsg", "docmethod ");
Syncmlstart ();
Return (* env)-> newstringutf (ENV, "Do C method OK! ");
}
File. c
# Include <stdio. h>
# Include <string. h>
# Include <sys/STAT. h>
# Include <dirent. h>
# Include <Android/log. h>
# Include "file. H"
/**
* File_open -- open a file
* Flag -- read/write/append
* Mode -- if not, whether to create
*/
Unsigned short file_open (file_handle ** filehandle, char * Name,
Unsigned short flag, unsigned short mode ){
If (filehandle = NULL ){
Return 100;
}
If (name = NULL ){
Return 100;
}
Char type [128] = "";
If (flag = fo_oread ){
If (mode! = Fo_create ){
Strcpy (type, "R"); // read-only, no File Creation
} Else {
Strcpy (type, "R + ");
}
} Else if (flag = fo_owrite ){
If (mode! = Fo_create ){
Strcpy (type, "W"); // write-only, no File Creation
} Else {
Strcpy (type, "W + ");
}
} Else if (flag = fo_rw ){
If (mode! = Fo_create ){
Strcpy (type, "");
} Else {
Strcpy (type, "A + ");
}
} Else if (flag = fo_append ){
If (mode! = Fo_create ){
Strcpy (type, "");
} Else {
Strcpy (type, "A + ");
}
}
* Filehandle = fopen (name, type );
Return 0;
}
/**
* File_close -- close the file
* Return Value
* 0 -- close successful; otherwise, Failure
*/
Unsigned short file_close (file_handle * filehandle ){
If (filehandle = NULL ){
Return 100;
}
Return fclose (filehandle );
}
/**
* file_getsize -- get the file length
* filesize -- return the file length
*/
unsigned short file_getsize (file_handle * filehandle, unsigned long * filesize) {
If (filehandle = NULL) {
return 100;
}< br> fseek (filehandle, 0l, seek_end );
* filesize = ftell (filehandle);
return 0;
}
/**
* file_read -- reads a file to the Buf
* count -- read length
* readcount -- returns the read length
* /
unsigned short file_read (file_handle * filehandle, char * Buf,
unsigned Long Count, unsigned long * readcount) {
If (filehandle = NULL) {
return 100;
}< br> * readcount = fread (BUF, 1, Count, filehandle);
_ android_log_print (android_log_info, "jnimsg ",
"file_read readcount = % d", * readcount);
return 0;
}
/**
* file_write -- write files from Buf
* count -- write length
* writecount -- return the written length
*/
unsigned short file_write (file_handle * filehandle, char * Buf,
unsigned Long Count, unsigned long * writecount) {
If (filehandle = NULL) {
return 100;
}< br> unsigned short write_result = fwrite (BUF, Count, 1, filehandle); // the return value is the number of successfully written projects.
If (write_result = 1) {
* writecount = write_result * count;
}< br> return write_result;
}
/**
* File_seek -- offset to the file
* Offset -- offset
* Origin -- offset direction
* Seeklen -- returns the offset length.
* Return value 0 -- success, other failures
*/
Unsigned short file_seek (file_handle * filehandle, long offset, short origin,
Unsigned long * seeklen ){
Unsigned short seek_result = fseek (filehandle, offset, origin );
If (seek_result = 0 ){
* Seeklen = offset;
}
Return seek_result;
}
/**
* File_delete -- delete an object
* 0 -- deletion successful-1 -- deletion failed
*/
Unsigned short file_delete (char * Name ){
If (name = NULL ){
Return 100;
}
Return remove (name );
}
/**
* File_isexist -- determines whether a file exists.
* 0 exists-1 does not exist
*/
Unsigned short file_isexist (char * path ){
If (Path = NULL ){
Return 100;
}
Return access (path, 0 );
}
/**
* File_create_dir -- create a file directory
* Return 0 -- Success
*/
Unsigned short file_create_dir (char * dirname ){
If (dirname = NULL ){
Return 100;
}
Return mkdir (dirname, s_irwxu );
}
/**
* File_delete_dir -- delete the file directory
*/
Unsigned short file_delete_dir (char * dirname ){
If (dirname = NULL ){
Return 100;
}
Dir * dp = NULL;
Dir * dpin = NULL;
Char * pathname = (char *) malloc (256 );
Memset (pathname, 0,256 );
Struct dirent * dirp;
Dp = opendir (dirname );
If (dp = NULL ){
_ Android_log_print (android_log_info, "jnimsg ",
"File_delete_dir your input directory is not exist! ");
Return 100;
}
While (dirp = readdir (DP ))! = NULL ){
If (strcmp (dirp-> d_name, "..") = 0 | strcmp (dirp-> d_name, ".") = 0)
Continue;
Strcpy (pathname, dirname );
Strcat (pathname ,"/");
Strcat (pathname, dirp-> d_name );
Dpin = opendir (pathname );
If (dpin! = NULL ){
Closedir (dpin );
Dpin = NULL;
File_delete_dir (pathname );
} Else {
Remove (pathname );
}
}
Rmdir (dirname );
Closedir (DP );
Free (pathname );
Pathname = NULL;
Dirp = NULL;
Return 0;
}
You can leave a message if you cannot understand it.