Android upload local image flash back

Source: Internet
Author: User

1. Phenomena

When we upload some local images of Android on the app, sometimes when we upload some images, we will somehow flash back, and check that there is no oom exception. Also upload some similar images can be used normally.

1.1 Turn on select local image
Intent intent = new Intent(Intent.ACTION_PICK, null);        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");startActivityForResult(intent, REQUEST_CODE_PICK_PHOTO);
1.2 Onactvityresult in the process of returning results
/ * Data is the third parameter of Onactvityresult * /URI uri = Data. GetData();String[] proj = {Mediastore. Images. Media. DATA};Cursor cursor = context. Getcontentresolver(). Query(URI, proj, null,null, NULL);int index = cursor. Getcolumnindexorthrow(Mediastore. Images. Media. DATA);Cursor. Movetofirst();String Path = cursor. getString(index);

This gives you the path to the picture. But here's the problem. When debugging, it is found that the reason for the flashback is that the cursor value is null. In other words, there is no such record at all in the current database. The following are examples of URLs for available pictures and flashback pictures

/* 可用url */content://media/external/images/media/117231由此url在内容提供者中查到的对应路径为:/storage/emulated/0/DCIM/Camera/IMG_20150624_180203.jpg/* 造成cursor为null的url  */file:///storage/emulated/0/MIUI/Gallery/cloud/.thumbnailFile/3a55c22cfe329906fcfbfe62f8b910bca4ad7d54.jpg显然,这并不是一个url,也不是一个直接可用的路径,所以cursor不会查到任何值。

The. thumbnailfile file in the path is the default picture cache file for a system. So, here I guess because it's a cache file, so the Mideastore content provider doesn't log the file's information and returns its path string.

2. Workaround

Since the path to the picture can be returned, we can bypass the content provider directly using this path. Note, however, that the path is similar to file:///storage/emulated ... Format, we are not able to use it directly, we need to remove its file://string and convert it to a path string that can be used.

String[] proj = {MediaStore.Images.Media.DATA}; cursor cursor = context.getcontentresolver (). Query (URI, Proj,NULL,NULL,NULL);//For some phones, the picture in the cache file, Mediastore no corresponding URL, then determine whether the incoming URL is similar to "file:///storage/emulated/0/..." Path//If it is a path, it is intercepted directly like/storage/emulated/0/. String and use this string as the pathif(Cursor = =NULL) {StringStr= Uri.tostring (); System.out.println (Str);if(Str. Contains ("file:///") {utility.closesafely (cursor);Str=Str. SUBSTRING (7);return Str; }}int Index= Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);    Cursor.movetofirst (); String Path = cursor.getstring (Index); utility.closesafely (cursor);returnPath

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android upload local image flash back

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.