QR code: Questions and Materials

Source: Internet
Author: User

Question 1: After removing a QR code image from your mobile phone or SD card, you can still see the preview thumbnail of the deleted image in the media repository.

Reference: Android: remove an image from sd card

Http://stackoverflow.com/questions/6707197/android-remove-an-image-from-sd-card


Question 2:

(new File(filePath)).deleteOnExit();

I always thought that deleteOnExit () was deleted when the file exists. This is a big mistake because I always regard this method as deleteonExist (), with only one letter missing. I finally found out today that I will not make this low-level mistake again in the future.

Correct file deletion should be implemented as follows:

File file = new File(filePath);if(file.exists())    file.delete();


Code snippet: Save the image to the file system

Private String getSaveDirectory () {if (! SdCardIsExist () {return context. getFilesDir (). getPath () + "/" + "user";} else {return ImageManager. USER_INFO_DIRECTORY;} private void deleteMyFile (String directory, String filename) {Logger. v (TAG, "invoke deleteMyFile"); File file = new File (directory, filename); if (file. exists () {Logger. v (TAG, "File is exist. begin to delete it. its path is> "+ file. getPath (); if (file. delete () {Logge R. v (TAG, "Delete success. ");} else {Logger. v (TAG, "Delete fail. ") ;}} else {Logger. v (TAG, "File is not exist. ") ;}} private void saveBitmapToFile (Bitmap source, String directory, String filename) {Logger. v (TAG, "invoke saveBitmapToFile"); OutputStream outputStream = null; try {File dir = new File (directory); if (! Dir. exists () dir. mkdirs (); File file = new File (directory, filename); Logger. v (TAG, "Begin to save bitmap to file which its path is>" + file. getPath (); outputStream = new FileOutputStream (file); if (source! = Null) {source. compress (CompressFormat. JPEG, 100, outputStream) ;}} catch (FileNotFoundException ex) {Log. w (TAG, ex);} finally {StreamUtil. release (outputStream) ;}} private void saveZxingImage () {Logger. v (TAG, "invoke saveZxingImage"); if (source = null) return; String filename = "user (" + user. getUsername () + "cmd.png"; String directory = getSaveDirectory (); Logger. v (TAG, "sdCardIsExist>" + sdCardIsExist (); // Delete the deleteMyFile (directory, filename); // Save the saveBitmapToFile (source, directory, filename) file ); // notify the media library to refresh sendBroadcast (new Intent (Intent. ACTION_MEDIA_MOUNTED, Uri. parse ("file: //" + Environment. getExternalStorageDirectory (); UIUtils. showToast (context, "saved QR code image succeeded ");}

The constant USER_INFO_DIRECTORY is:

public static final String USER_INFO_DIRECTORY =        Environment.getExternalStorageDirectory().toString() + "/ivideo/user";

Determine whether the SD card has code implementation

private boolean sdCardIsExist() {    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);}

Code snippet: Get and parse the QR code image from the media Repository

If (requestCode = SELECT_ZXING_PICTURE) {Logger. v (TAG, "return back from media store. "); if (resultCode = RESULT_ OK) {try {Uri dataUri = data. getData (); Logger. v (TAG, "open input stream from data which its URI is>" + dataUri. toString (); InputStream istream = getContentResolver (). openInputStream (dataUri); if (zxingBitmap! = Null) {Logger. v (TAG, "zxing bitmap is not null, begin to recycle it. "); zxingBitmap. recycle ();} else {Logger. v (TAG, "zxing bitmap is null. ");} Logger. v (TAG, "decode stream from input stream. "); zxingBitmap = BitmapFactory. decodeStream (istream); Logger. v (TAG, "get user infomation from zxing bitmap. "); String zxingInfo = getUserFromBitmap (zxingBitmap); Logger. v (TAG, "login with ukey>" + zxin GInfo); loginFromZxingInfo (zxingInfo);} catch (FileNotFoundException e) {Logger. e (TAG, "caught exception about file not found. "); Logger. e (TAG, e); showToast ("file not found");} catch (OutOfMemoryError err) {Logger. e (TAG, "caught error about of memory. "); Logger. e (TAG, err); showToast ("parsing error");} catch (NotFoundException e) {Logger. e (TAG, "caught exception about zxing not found. "); Logger. e (TAG, e) ;} Catch (ChecksumException e) {Logger. e (TAG, "caught exception about zxing check sum. "); Logger. e (TAG, e);} catch (FormatException e) {Logger. e (TAG, "caught exception about zxing format. "); Logger. e (TAG, e);} if (zxingBitmap! = Null) {Logger. v (TAG, "zxing bitmap is not null, begin to recycle it."); zxingBitmap. recycle (); zxingBitmap = null ;}} return ;}

Problem: if the phone is not shut down, remove the SD card from the phone and do not restart the phone. The program checks whether the SD card exists, and the result shows that the SD card still exists. As a result, the QR code image cannot be saved. Restart the phone, and then open the program. If the SD card segment in the program exists, the result will show that the SD card does not exist.


Problem: When the SD card does not exist, the program saves the QR code image to the mobile phone and then opens the media library. Result: The media library cannot display the image on the mobile phone. Possible cause: after saving the QR code, the system only notifies you to scan the SD card again without scanning the mobile phone.


Problem: When the SD card does not exist, save the QR code image to the directory data/xxx/files/user/xxx.png on the phone memory card, and then call the chmod command to modify the file permissions, the operation that has been verified to modify the permission has been successfully executed. In File Explorer, you can see that the permission for this File is-rwxrwxrwx, or 777. However, the png file cannot be previewed in the mobile phone library application.


Problem: When the SD card is uninstalled or mounted at startup, and the mobile phone is not restarted, the media library does not listen. Therefore, the following situations may occur: the mobile phone does not have an SD card at the beginning. After the SD card is mounted to the mobile phone and the media repository application is enabled, the application still displays that the SD card does not exist or the SD card is unavailable.


Problem: the best way to check whether the SD card is available. The actual effect cannot be detected using methods such as Environment. getExternalStorageState () and Environment. getExternalStorageDirectory (). canWrite. After the SD card is unplugged, canWrite () returns true, and getExternalStorageState () returns Environment. MEDIA_MOUNTED. The best way is to create a file on SD. If the file can be created successfully, the SD card is available, and vice versa.

Code snippet: checks whether the SD card is available

private boolean sdCardIsAvailable() {    String fileName = new Date().getTime() + ".test";    String filePath =  Environment.getExternalStorageDirectory() + "/" + fileName;    File file = new File(filePath);    try {        if(file.createNewFile()) {            Logger.v(TAG, "create new file success. filepath >> " + filePath);        } else {            Logger.v(TAG, "create new file fail. filepath >> " + filePath);            return false;        }        if(file.exists()) {            if(file.delete()) {                Logger.v(TAG, "delete file success. filepath >> " + filePath);            } else {                Logger.v(TAG, "delete file fail. filepath >> " + filePath);                return false;            }        }    } catch (IOException e) {        Logger.v(TAG, "caught " + e.getClass().getName() + " >> " + e.getMessage());        return false;    }    return true;}

Code snippet: Call the above method in the program to check whether the SD card is available

If (! SdCardIsAvailable () {UIUtils. showToast (context, "SD card unavailable"); Logger. v (TAG, "sd card is unavailable. "); return;} else {Logger. v (TAG, "sd card is available. ");}

Debug log: When the sd card is unavailable, the sd card is printed as unavaliable.

11-15 18:21:32.019: V/SaveZxingActivity(15205): caught java.io.IOException >> I/O error11-15 18:21:32.029: V/SaveZxingActivity(15205): sd card is unavailable.


References:

1. android-based SD card mounting and unmounting listening

Http://blog.csdn.net/guoying_/article/details/6618277

2. android listens to SDCard installed and uninstalled code snippets and passes the test)

Http://www.cnblogs.com/error404/archive/2011/09/08/2170998.html

3. android SD card status, path, available space, memory

Http://blog.csdn.net/ureygo/article/details/7064030


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.