i have app upload , download image server, code upload image gallery server. want reduce image size before uploading image. how it?
private void uploadmap(final string sfile){ int fbyte,buffersize,cbuffer; int maxbuffer=1024*1024; file f=new file(sfile); try{ fileinputstream fis=new fileinputstream(f); /////////////////////////// url url=new url(main.url+"uploadmap.php"); /////////////////////////////////////////////// httpurlconnection con=(httpurlconnection) url.openconnection(); con.setdoinput(true); con.setdooutput(true); con.setrequestmethod("post"); con.setusecaches(false); con.setrequestproperty("connection", "keep-alive"); con.setrequestproperty("enctype", "multipart/form-data"); con.setrequestproperty("content-type", "multipart/form-data; boundary=*****"); con.setrequestproperty("uploaded-file", sfile); dataoutputstream dos =new dataoutputstream(con.getoutputstream()); dos.writebytes("--*****\r\ncontent-disposition: form-data; name=\"uploaded_file\"; filename=\""+sfile+"\"\r\n\r\n"); fbyte=fis.available(); buffersize=math.min(fbyte, maxbuffer); byte[] buffer=new byte[buffersize]; cbuffer=fis.read(buffer,0,buffersize); while(cbuffer>0){ dos.write(buffer,0,buffersize); fbyte=fis.available(); buffersize=math.min(fbyte, maxbuffer); cbuffer=fis.read(buffer,0,buffersize); } dos.writebytes("\r\n--*****--\r\n"); if(con.getresponsecode()==200){ runonuithread(new runnable(){ @override public void run() { maptv.settext(getname(sfile)); dialog.dismiss(); sendmap.setvisibility(view.gone); toast.maketext(getapplicationcontext(), "uploaded", toast.length_long).show(); } }); fis.close(); dos.flush(); dos.close(); } }catch(final exception e){ runonuithread(new runnable(){ @override public void run() { pr.dismiss(); toast.maketext(getapplicationcontext(), "not upload", toast.length_long).show(); } }); }
i want reduce image size code upload , dont want reduce image quality.
just copy & paste following method compress image before send server:
public string compressimage(string imageuri) { string filepath = getrealpathfromuri(imageuri); bitmap scaledbitmap = null; bitmapfactory.options options = new bitmapfactory.options(); // setting field true, actual bitmap pixels not loaded in memory. bounds loaded. if // try use bitmap here, null. options.injustdecodebounds = true; bitmap bmp = bitmapfactory.decodefile(filepath, options); int actualheight = options.outheight; int actualwidth = options.outwidth; // max height , width values of compressed image taken 816x612 float maxheight = 816.0f; float maxwidth = 612.0f; float imgratio = actualwidth / actualheight; float maxratio = maxwidth / maxheight; // width , height values set maintaining aspect ratio of image if (actualheight > maxheight || actualwidth > maxwidth) { if (imgratio < maxratio) { imgratio = maxheight / actualheight; actualwidth = (int) (imgratio * actualwidth); actualheight = (int) maxheight; } else if (imgratio > maxratio) { imgratio = maxwidth / actualwidth; actualheight = (int) (imgratio * actualheight); actualwidth = (int) maxwidth; } else { actualheight = (int) maxheight; actualwidth = (int) maxwidth; } } // setting insamplesize value allows load scaled down version of original image options.insamplesize = calculateinsamplesize(options, actualwidth, actualheight); // injustdecodebounds set false load actual bitmap options.injustdecodebounds = false; // options allow android claim bitmap memory if runs low on memory options.inpurgeable = true; options.ininputshareable = true; options.intempstorage = new byte[16 * 1024]; try { // load bitmap path bmp = bitmapfactory.decodefile(filepath, options); } catch (outofmemoryerror exception) { exception.printstacktrace(); } try { scaledbitmap = bitmap.createbitmap(actualwidth, actualheight, bitmap.config.argb_8888); } catch (outofmemoryerror exception) { exception.printstacktrace(); } float ratiox = actualwidth / (float) options.outwidth; float ratioy = actualheight / (float) options.outheight; float middlex = actualwidth / 2.0f; float middley = actualheight / 2.0f; matrix scalematrix = new matrix(); scalematrix.setscale(ratiox, ratioy, middlex, middley); canvas canvas = new canvas(scaledbitmap); canvas.setmatrix(scalematrix); canvas.drawbitmap(bmp, middlex - bmp.getwidth() / 2, middley - bmp.getheight() / 2, new paint(paint.filter_bitmap_flag)); // check rotation of image , display exifinterface exif; try { exif = new exifinterface(filepath); int orientation = exif.getattributeint( exifinterface.tag_orientation, 0); log.d("exif", "exif: " + orientation); matrix matrix = new matrix(); if (orientation == 6) { matrix.postrotate(90); log.d("exif", "exif: " + orientation); } else if (orientation == 3) { matrix.postrotate(180); log.d("exif", "exif: " + orientation); } else if (orientation == 8) { matrix.postrotate(270); log.d("exif", "exif: " + orientation); } scaledbitmap = bitmap.createbitmap(scaledbitmap, 0, 0, scaledbitmap.getwidth(), scaledbitmap.getheight(), matrix, true); } catch (ioexception e) { e.printstacktrace(); } fileoutputstream out = null; string filename = getfilename(); try { out = new fileoutputstream(filename); // write compressed bitmap @ destination specified filename. scaledbitmap.compress(bitmap.compressformat.jpeg, 80, out); } catch (filenotfoundexception e) { e.printstacktrace(); } return filename; } public string getfilename() { file file = new file(environment.getexternalstoragedirectory().getpath(), "myfolder/images"); if (!file.exists()) { file.mkdirs(); } string uristing = (file.getabsolutepath() + "/" + system.currenttimemillis() + ".jpg"); return uristing; } private string getrealpathfromuri(string contenturi) { uri contenturi = uri.parse(contenturi); cursor cursor = getcontentresolver().query(contenturi, null, null, null, null); if (cursor == null) { return contenturi.getpath(); } else { cursor.movetofirst(); int index = cursor.getcolumnindex(mediastore.images.imagecolumns.data); return cursor.getstring(index); } } public int calculateinsamplesize(bitmapfactory.options options, int reqwidth, int reqheight) { final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1; if (height > reqheight || width > reqwidth) { final int heightratio = math.round((float) height / (float) reqheight); final int widthratio = math.round((float) width / (float) reqwidth); insamplesize = heightratio < widthratio ? heightratio : widthratio; } final float totalpixels = width * height; final float totalreqpixelscap = reqwidth * reqheight * 2; while (totalpixels / (insamplesize * insamplesize) > totalreqpixelscap) { insamplesize++; } return insamplesize; }
Comments
Post a Comment