These two days in the project, do upload picture function piece, encounter two problems, one is how to get the path of the selected picture, one is how to compress the picture, after checking some information and read someone else to write after finally toss out, in this record.
First of all, since we want to select pictures, we have to get all the pictures locally, and Android has encapsulated the intent for us.
1 New null); // Select an item from the list and return all data 2 Intent.setdataandtype (3 MediaStore.Images.Media.EXTERNAL_CONTENT_URI,// get all pictures of the system 4 "image/*"); // type of picture, image/* for all types of pictures 5 Startactivityforresult (Intent, photo_gallery);
Then we rewrite the onactivityresult method.
After Android1.5 the system will call the Mediascanner service for background scanning, index songs, pictures, videos and other information, and save the data in Android.provider.MediaStore.Images.Thumbnails And android.provider.MediaStore.Video.Thumbnails in these two databases.
So we need to get the data from the data using the activity.managedquery (URI, projection, selection, Selectionargs, SortOrder) method.
URI: The resource index that needs to be returned
Projection: Used to identify what data needs to be included in the return data.
Selection: A filter parameter that matches the criteria of a query, similar to the conditional judgment in the Where in the SQL statement.
Selectionargs: Ibid.
SortOrder: Sorts the return information.
1 @Override2 protected voidOnactivityresult (intRequestcode,intResultCode, Intent data)3 {4 Switch(Requestcode)5 {6 //Request for access to this map product7 CasePhoto_gallery:8 { 9 //image information should be included in the return dataTenString[] proj ={MediaStore.Images.Media.DATA}; One //gets the cursor object that contains the data you want A@SuppressWarnings ("Deprecation") -cursor cursor = managedquery (Data.getdata (), Proj,NULL,NULL,NULL); - //Get Index the intPhotocolumn =Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA); - //always start the cursor - Cursor.movetofirst (); - //get picture path based on index value +String Path =cursor.getstring (photocolumn); - + A Break; at } - - default: - Break; -}
Above, we can get the local picture path, then our team image compression processing.
1 //first, the selected picture is converted into a stream form, and the path gets2FileInputStream is =NewFileInputStream (path);3 //defines a file, as a compressed picture4File f =NewFile ("Picture Save Path", "Picture name");5 intSize = "";6Options options =NewOptions ();7Options.insamplesize =size;8 //reduce the image to the original 1/size, or it will report a memory overflow error when the picture is large9Bitmap image = Bitmapfactory.decodestream (InputStream,NULL, options);Ten One is.close (); A -Bytearrayoutputstream BAOs =NewBytearrayoutputstream (); -Image.compress (Bitmap.CompressFormat.JPEG, BAOs);//here 100 means no compression, and the uncompressed data is stored in the BAOs. the intper = 100; - while(Baos.tobytearray (). length/1024 > 500) {//loop to determine if the image is larger than 500kb after compression -Baos.reset ();//Reset BAOs is empty BAOs -Image.compress (Bitmap.CompressFormat.JPEG, per, BAOs);//compress the image to the original (100-per)% and store the compressed data in the BAOs +per-= 10;//each time, it's reduced by ten - + } A //Recycle pictures, clean up memory at if(Image! =NULL&&!image.isrecycled ()) { - image.recycle (); -Image =NULL; - System.GC (); - } -Bytearrayinputstream ISBM =NewBytearrayinputstream (Baos.tobytearray ());//Store the compressed data BAOs in the Bytearrayinputstream in btout.close (); - fileoutputstream os; toOS =NewFileOutputStream (f); + //Customizing the tool class to copy the input stream into the output stream - Streamtransferutils.copystream (btinput, OS); the btinput.close (); *Os.close ();
Once finished, we can see the compressed picture under the specified picture save path.
Android get local image and compress method