Method for android to identify U disk and read and write files

  • 2021-10-11 19:31:45
  • OfStack

There are many requirements for reading and writing files in android, including identifying U disk, ejecting U disk, reading and writing files in U disk, etc.

So, how to realize these requirements? The author simply said:

1. Identify the U disk:

The method of identifying U disk is as long as two android official classes are used.

They are ContentResolver and AsyncQueryHandler. The former provides content for the latter to read.

The practice is as follows:

1) Gets the state and traverses


contentObserver = new ContentObserver(mHandler) {
   @Override
   public void onChange(boolean selfChange, Uri uri) {
    super.onChange(selfChange, uri);
    mHandler.removeCallbacks(arg.runnable);
    mHandler.postDelayed(arg.runnable, DELAY);
   }
  };

runnable = new Runnable() {
   @Override
   public void run() {
    mAsyncQueryHandler.startQuery(token, null, uri, projection, selection, selectionArgs,
      orderBy);
   }
  };

2) Callback data to UI


  mAsyncQueryHandler = new AsyncQueryHandler(mCR) {
   @Override
   protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
    super.onQueryComplete(token, cookie, cursor);
    if (!mArgs.isEmpty() && token < mArgs.size()) {
     QueryArg arg = mArgs.get(token);
     if (mCallback != null) {
      mCallback.QueryNotify(arg.uri, cursor);
     }
     if (!cursor.isClosed()) {
      cursor.close();
     }
    }
   }
  };

Simply put, onchanged will be called once when U disk is mounted or unplugged, and this method will be called during this period, because we can call the execution thread to traverse the data at this time, so the number of data to U disk and files, followed by QueryArg is a custom class, which is used to encapsulate the data we need for distinction. This class is also posted below


 protected static final class QueryArg {
  public Uri uri;
  public String[] projection;
  public String selection;
  public String[] selectionArgs;
  public String orderBy;
  public ContentObserver contentObserver;
  public Runnable runnable;
  public Object cookie;
 }

2. Eject the U disk safely

The method of popping up U disk is very simple, which is basically similar to deleting database data.

1)


ContentResolver cr = mContext.getContentResolver();

2)


  ContentValues values = new ContentValues();
  values.put(MediaStore.MediaDevice.FIELD_VALID, false);
cr.update(MediaStore.MediaDevice.CONTENT_URI, values, where, selectionArgs);

In this way, the U disk can be ejected. Note: U disk in their own software pop-up only changes the software U disk status, no impact on the system. The main reason is that the Uri provided at update only affects the content provider of the current package name.

To its own software in the pop-up state without re-plugging under the premise of reloading the author temporarily did not see the relevant methods, if please inform, thank you.

3. Read and write U disk files

In the previous 1, there is a method to identify U disk, which can customize 1 callback when traversing.


QueryNotify ( Uri uri, Cursor cursor ) 

Of course, the number of parameters or the name of callback can be customized. The author has two parameters here, the first one is mainly used to distinguish types, and the second one is to obtain data.

There is already cursor here, so it is not difficult, and it is not how bb reads data here.

Simply talk about how to write data.

The method of writing data is similar to that of popping up U disk, both through


ContentResolver <pre name="code" class="html">ContentValues 

These two classes are implemented.


mContext.getContentResolver().update(
          MediaStore.getContentUri(MediaStore.MediaBase.TABLE_NAME, info.id), values,
          "_id=", new String[] { info.id + "" });

In this way, the requirement of changing file attributes is met.

In this way, we can roughly complete the requirements related to U disk reading and writing recognition. Of course, this article is also a little taste, and many details should be tried and improved by ourselves.


Related articles: