Android Bluetooth File Receive path modification method

Source: Internet
Author: User

Tag: Android Bluetooth File receive path Bluetooth receive

To modify a file:

Packages/apps/bluetooth/src/com/android/bluetooth/opp/bluetoothoppreceivefileinfo.java


Related Code snippet:

    public static Bluetoothoppreceivefileinfo Generatefileinfo (context context, int id) {Contentresolver contentre        Solver = Context.getcontentresolver ();        Uri Contenturi = uri.parse (Bluetoothshare.content_uri + "/" + ID);        String filename = NULL, hint = NULL, MimeType = NULL;        Long length = 0;        Cursor metadatacursor = null; try {metadatacursor = Contentresolver.query (Contenturi, new string[] {bluetoothshare.filename_        HINT, Bluetoothshare.total_bytes, bluetoothshare.mimetype}, NULL, NULL, NULL);            } catch (Sqliteexception e) {if (metadatacursor! = null) {metadatacursor.close ();            } metadatacursor = null;        LOG.E (Constants.tag, "Generatefileinfo:" + e);            } catch (Cursorwindowallocationexception e) {metadatacursor = null;        LOG.E (Constants.tag, "Generatefileinfo:" + e);     } if (metadatacursor! = null) {       try {if (Metadatacursor.movetofirst ()) {hint = metadatacursor.getstring (0);                    Length = Metadatacursor.getlong (1);                MimeType = metadatacursor.getstring (2);                }} finally {Metadatacursor.close ();                if (V) log.v (Constants.tag, "freeing cursor:" + metadatacursor);            Metadatacursor = null;        }} File base = null;        StatFs stat = null; if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {String root = Environment.getext            Ernalstoragedirectory (). GetPath ();            base = new File (root + constants.default_store_subdir); if (!base.isdirectory () &&!base.mkdir ()) {if (D) log.d (Constants.tag, "Receive File Aborted-can                ' t create base directory ' + Base.getpath ()); return new Bluetoothoppreceivefileinfo (BlueTOOTHSHARE.STATUS_FILE_ERROR);        Stat = new StatFs (Base.getpath ());            } else {if (D) log.d (Constants.tag, "Receive File aborted-no External storage");        return new Bluetoothoppreceivefileinfo (Bluetoothshare.status_error_no_sdcard); }/* * Check whether there ' s enough space on the target filesystem to save * the file.         Put a bit of margin (in case creating the file grows the * system by a few blocks). */if (stat.getblocksize () * (long) stat.getavailableblocks ()-4) < length) {if (D) log.d (Constants .            TAG, "Receive File aborted-not enough free space");        return new Bluetoothoppreceivefileinfo (bluetoothshare.status_error_sdcard_full);        } filename = choosefilename (hint); if (filename = = null) {//should not happen.        It must be pre-rejected return new Bluetoothoppreceivefileinfo (Bluetoothshare.status_file_error);  }      String extension = null;        int dotindex = Filename.lastindexof ("."); if (Dotindex < 0) {if (MimeType = = null) {//should not happen.            It must be pre-rejected return new Bluetoothoppreceivefileinfo (Bluetoothshare.status_file_error);            } else {extension = "";            }} else {extension = filename.substring (Dotindex);        filename = filename.substring (0, Dotindex); } if ((filename! = null) && (Filename.getbytes (). length > Opp_length_of_file_name)) {/* includ ing Extn of the file, Linux supports 255 character as a maximum length of the * file name to be created. Hence, Instead of sending Obex_http_internal_error, * as a response, truncate the length of the file name and SA ve it.           This check majorly * helps in the "vcard", where Phone book app supports contact name to be saved * More than 255 charActers, but the server rejects the card just because the length of * VCF file name received exceeds 255 CHARACTE           Rs.              */try {byte[] Oldfilename = filename.getbytes ("UTF-8");              byte[] NewFileName = new Byte[opp_length_of_file_name];              System.arraycopy (oldfilename, 0, NewFileName, 0, Opp_length_of_file_name);          filename = new String (NewFileName, "UTF-8");          } catch (Unsupportedencodingexception e) {log.e (Constants.tag, "Exception:" + e); } if (D) log.d (Constants.tag, "File name is too long.        Name is truncated as: "+ filename);        } filename = Base.getpath () + file.separator + filename;        Generate a unique filename, create the file, return it.        String fullfilename = chooseuniquefilename (filename, extension);            if (!safecanonicalpath (fullfilename)) {//If this second check fails and then we better reject the transfer return new Bluetoothoppreceivefileinfo (Bluetoothshare.status_file_error);        } if (V) log.v (Constants.tag, "Generated received filename" + fullfilename);                if (fullfilename! = null) {try {new FileOutputStream (Fullfilename). Close ();                int index = Fullfilename.lastindexof ('/') + 1; Update Display Name if (Index > 0) {String displayName = fullfilename.substring (i                    NDEX);                    if (V) log.v (Constants.tag, "New display name" + displayName);                    Contentvalues updatevalues = new Contentvalues ();                    Updatevalues.put (Bluetoothshare.filename_hint, displayName);                Context.getcontentresolver (). Update (Contenturi, updatevalues, NULL, NULL);                        } return new Bluetoothoppreceivefileinfo (fullfilename, Length, New FileOutputStream (            Fullfilename), 0);        } catch (IOException e) {        if (D) log.e (Constants.tag, "Error when creating file" + Fullfilename);            return new Bluetoothoppreceivefileinfo (Bluetoothshare.status_file_error);        }} else {return new bluetoothoppreceivefileinfo (Bluetoothshare.status_file_error);  }} private static Boolean Safecanonicalpath (String uniquefilename) {try {File receivefile = new            File (Uniquefilename); if (Sdesiredstoragepath = = null) {Sdesiredstoragepath = Environment.getexternalstoragedirectory (). GetPath (            ) + Constants.default_store_subdir;            } String Canonicalpath = Receivefile.getcanonicalpath (); Check if canonical path is Complete-case sensitive-wise if (!canonicalpath.startswith (Sdesiredstoragepath)            ) {return false;        } return true; } catch (IOException IoE) {//If an exception are thrown, there might be something WRong with the file.        return false; }    }


Modification Method:

Root is the root directory of the store.

If you need to modify the external storage, modify the assignment here.

If you need to modify subdirectories, modify the concatenation assignment of base.


It is important to note that if you modify root or base, you need to reassign the Sdesiredstoragepath to make it consistent, otherwise it will cause the Safecanonicalpath () judgment to fail.
You need to make the full path legal, otherwise you cannot receive the file.


Android Bluetooth File Receive path modification method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.