before the Android 4.4 system, select the picture and get the picture path code as follows:
Access Gallery
Intent Intent = new Intent ();
Intent.settype ("image/*");/can choose picture Video
intent.setaction (intent.action_get_content);
Startactivityforresult (Intent, Select);
Get Picture Path
Method overload gets the return value @Override protected void onactivityresult (int requestcode, int resultcode, Intent data) {super.onactivity
Result (Requestcode, ResultCode, data);
Switch (requestcode) {case Select:if (ResultCode = = RESULT_OK) {/**
* When the selected picture is not empty, the way to get the picture/URI uri = Data.getdata ();
String Ddd=uri.decode (uri.tostring ());
try {string[] Pojo = {MediaStore.Images.Media.DATA};
Cursor Cursor = Managedquery (URI, pojo, NULL, NULL, NULL);
if (cursor!= null) {Contentresolver CR = This.getcontentresolver ();
int colunm_index = Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);
Cursor.movetofirst ();
String path = cursor.getstring (Colunm_index); The above code gets the picture path/*Gets the file's extension */file F2 = new file (path); String fName2 = F2.getname ();//file name string extendName1 = Fname2.substring (Fname2.lastindexof (".") + 1)///extension///Copy to the new name//String fileName1 = file in the specified folder//The SimpleDateFormat = new ("Y Yyymmddhhmmss "). Format (new Date ()) +". "+ extendname1;//after import picture name String Totalname = long.tostring (
System.currenttimemillis ()) + "_" + Casename + "_" + filename+ "_" + userName;
String fileName1 = Totalname + "." + extendName1;
String filename=str.substring (Str.lastindexof ("/") +1,str.length ());
/* Create a new folder to store import files/Pastepath = MPath + "import/";
File out = new file (Pastepath);
if (!out.exists ()) {out.mkdirs ();
Storage path of//file String Newpathone = Pastepath + fileName1; Boolean res = CopyFile (path, newpathone);//copy audio file to specified folder/* Copy another file to the directory that will not be deleted * * Im
Portpath = NewPath + "import/";
File Outfileonew = new file (Importpath);
if (!outfileonew.exists ()) {outfileonew.mkdirs ();
}//file's storage path String Newpaththree = Importpath + fileName1; Boolean result = CopyFile (path, newpaththree);//Copy the recording file to the specified folder if (result = = True && ; res = = true) {Toast.maketext (this, "the import was successful.)
", Toast.length_short). Show ();
A break in the catch (Exception e) {}; }
}
}
Android 4.4 System and above, the picture path obtained using the above method is empty for the following reasons:
Starting with the Android 4.4 (including), after accessing the gallery in the above way, the URI returned is as follows (access "recent"):
Uri is:content://com.android.providers.media.documents/document/image%3a188382 Uri.getpath is:/document/image : 188383 The corresponding picture true path:/storage/emulated/0/pictures/screenshots/screenshot_2014-09-22-21-40-53.png
not only that, for different types of libraries, the returned URI form is not the same (access to a normal photo album):
Uri is:content://media/external/images/media/188222 Uri.getpath is:/external/images/media/188223 corresponding picture true path:/ Storage/emulated/0/download/20130224235013.jpg
the URI returned before 4.4 has only one form, as follows:
Uri is:content://media/external/images/media/140462 Uri.getpath is:/external/images/media/140463 corresponding picture true path:/ Storage/emulated/0/dcim/camera/20130224235013.jpg
Therefore, on Android 4.4 or later devices, a picture database with a simple getdatacolumn (context, Uri, NULL, NULL) does not satisfy all the requirements, so it needs to be differentiated according to different types when acquiring the true path of a picture.
for android4.4 and above choose picture Solution:
method One:
Intent Intent = new Intent ();
Intent.settype ("image/*");/optional Picture video
Modify to the following two lines of code
Intent.setaction (Intent.action_pick);
Intent.setdata (Android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//Use the above mode and add the above two sentences
Startactivityforresult (Intent, Select);
Using the above method, you can get to the path using the method in the android4.4 below Onactivityresult, which does not differentiate between system versions.
Method Two:
When you select a picture in Android, open the album Selection picture (depending on whether it is 4.4 and above the different action), the code is as follows:
if (Android.os.Build.VERSION.SDK_INT >= Android.os.Build.VERSION_CODES. KitKat) {//4.4 and above intent2.setaction (intent.action_open_document);
} else {//4.4 The following
intent2.setaction ( intent.action_get_content);
}
The URI URI that is returned in the Onactivityresult = Data.getdata (); If the android4.4 URI format is content://com.android.providers.media.documents /document/image:3952,4.4 the following format
content://media/external/images/media/3951, to get the image storage path needs, according to the system version of the use of different methods to obtain the code as follows:
if Android.os.Build.VERSION.SDK_INT >=.
KitKat) {//4.4 and above String Wholeid = Documentscontract.getdocumentid (Contenturi);
String id = wholeid.split (:) [1];
string[] column = {MediaStore.Images.Media.DATA};
String sel = mediastore.images.media._id + =?; Cursor Cursor = Context.getcontentresolver (). Query (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, SEL,
New string[] {ID}, NULL);
int columnindex = Cursor.getcolumnindex (column[0]);
if (Cursor.movetofirst ()) {FilePath = cursor.getstring (columnindex);
} cursor.close ();
}else{//4.4 below, that is, more than 4.4 ways to get the path string[] projection = {MediaStore.Images.Media.DATA};
Cursor Cursor = Context.getcontentresolver (). Query (Contenturi, projection, NULL, NULL, NULL);
int column_index = Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);
Cursor.movetofirst ();
FilePath = cursor.getstring (Column_index); }
* * Note: In general the second method of effect on the 4.4 system interface looks very good, if only to function the first kind of simple.
Reference URL:
[Http://y.dobit.top/Detail/61.html]
[Http://blog.sina.com.cn/s/blog_88be6cd00102uzu8.html]
[Http://www.bubuko.com/infodetail-831974.html]
[http://blog.163.com/shexinyang@126/blog/static/13673931220149135409328/]
Key code:
if (Android.os.Build.VERSION.SDK_INT >= Android.os.Build.VERSION_CODES. KitKat) {//4.4 and above
intent2.setaction (intent.action_open_document);
} else {//4.4 The following
intent2.setaction ( intent.action_get_content);
}
@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (reque
Stcode, ResultCode, data); Switch (requestcode) {case photo_request_takephoto://call Preftool.setbooleansave when you choose to take a picture (this, prefs.pr
E_not_to_backgroud, false);
if (ResultCode = = ACTIVITY.RESULT_OK) {piclist.add (picname);
Refreshlistview ();
} break;
Case photo_request_gallery://The Preftool.setbooleansave (this, Prefs.pre_not_to_backgroud, false) when choosing to obtain a picture locally;
if (ResultCode = = activity.result_ok) {uri uri = Data.getdata ();
String Imgpath = ""; if (Android.os.Build.VERSION.SDK_INT >= Android.os.Build.VERSION_CODES.
KitKat) {//4.4 and above String Wholeid = Documentscontract.getdocumentid (URI);
String id = wholeid.split (":") [1]; string[] Column = {MedIaStore.Images.Media.DATA};
String sel = mediastore.images.media._id + "=?"; Cursor Cursor = Groupcontext.getapplication (). Getcontentresolver (). Query (MediaStore.Images.Media.EXTERNAL_CONTENT
_uri, column, SEL, new string[]{id}, NULL);
int columnindex = Cursor.getcolumnindex (column[0]);
if (Cursor.movetofirst ()) {Imgpath = cursor.getstring (columnindex);
} cursor.close ();
else {//4.4 below, that is, more than 4.4 methods to get the path Cursor Cursor = Getcontentresolver (). Query (URI, NULL, NULL,
NULL, NULL);
Cursor.movetofirst ();
Cursor.movetofirst (); String Imgno = cursor.getstring (0); Picture number Imgpath = cursor.getstring (1); Picture file path String imgsize = cursor.getstring (2);
Picture size String imgname = cursor.getstring (3);
Picture filename cursor.close ();
String picname = Dateutil.format (New Date (), "YYYYMMDDHHMMSS") + ". jpg";
Filecache.copyfile (Imgpath, Files.photopath + picname);
Piclist.add (Picname);
Refreshlistview ();
} break;
}
}