Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK !!!! For the correct way follow this this code ! </p> <p>BUT : becarful, <strong>it's an example</strong> ! -> <strong>YOU SHOULDN'T DO A INTERNET REQUEST IN THE MAIN THREAD</strong></p> <p>for execute this code, the function <code>exec();</code> should to be put into a "doInBackground()" of an <code>asyncTask&lt;Object, Object, Object&gt;();</code></p> <p>the <code>startActivityForResult()</code> and the <strong>override</strong> of <code>onActivityResult()</code> should to be into an activity class</p> <p>tell me if it's correct !!!!</p> <pre><code>private int ACTIVITY_ID_PICK_PHOTO = 42; private int maxWidth = 350; private int maxHeight = 350; private String url = "http://ip/serverApp/upload/transfert.php" //Call the activity for select photo into the gallery private void SelectPhoto(){ Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, ACTIVITY_ID_PICK_PHOTO); } // check the return of the result @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //check th id of the result if (requestCode == ACTIVITY_ID_PICK_PHOTO) selectPhotoControl(data); } //Working data private void selectPhotoControl(Intent data) { //check if any photo is selected if (data == null) return; //get the uri of the picture selected Uri photoUri = data.getData(); if (photoUri != null) { //decode the Uri String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); //get the uri of the image String filePath = cursor.getString(columnIndex); cursor.close(); //get the image in the bitmap and resize the image Bitmap bp = resize(filePath); if (bp != null) postImage(bp, filePath); } } public static HttpResponse postImage(Bitmap bp, String uristr) throws ClientProtocolException, IOException { //initialization of the postrequest HttpPost httpPost = new HttpPost(url); //create the multipart entitiy (if you want send another content) MultipartEntity entity = new MultipartEntity( //the boundary for separate the informations HttpMultipartMode.BROWSER_COMPATIBLE, "------CustomBoundary", null); if (bp != null) { //create the bytes array for send the image ByteArrayOutputStream bos = new ByteArrayOutputStream(); //if you want to compress the image -&gt; write the result into bos bp.compress(CompressFormat.JPEG, 100, bos); //get the filename of the image String filename = uristr.substring(uristr.lastIndexOf("/") + 1, uristr.length()); //put the picture into the body of this part FormBodyPart fbp = new FormBodyPart("photo", new ByteArrayBody( bos.toByteArray(), "image/jpeg", filename)); //add the part to the entity entity.addPart(fbp); } //set the entity into the request httpPost.setEntity(entity); //execute the request return exec(httpPost); } protected synchronized static HttpResponse exec(HttpRequestBase base) throws ClientProtocolException, IOException{ if (base != null) //Execute the request return mHttpClient.execute(base); else return null; } private Bitmap resize(String path){ // create the options BitmapFactory.Options opts = new BitmapFactory.Options(); //just decode the file opts.inJustDecodeBounds = true; Bitmap bp = BitmapFactory.decodeFile(path, opts); //get the original size int orignalHeight = opts.outHeight; int orignalWidth = opts.outWidth; //initialization of the scale int resizeScale = 1; //get the good scale if ( orignalWidth &gt; maxWidth || orignalHeight &gt; maxHeight ) { final int heightRatio = Math.round((float) orignalHeight / (float) maxHeight); final int widthRatio = Math.round((float) orignalWidth / (float) maxWidth); resizeScale = heightRatio &lt; widthRatio ? heightRatio : widthRatio; } //put the scale instruction (1 -&gt; scale to (1/1); 8-&gt; scale to 1/8) opts.inSampleSize = resizeScale; opts.inJustDecodeBounds = false; //get the futur size of the bitmap int bmSize = (orignalWidth / resizeScale) * (orignalHeight / resizeScale) * 4; //check if it's possible to store into the vm java the picture if ( Runtime.getRuntime().freeMemory() &gt; bmSize ) { //decode the file bp = BitmapFactory.decodeFile(path, opts); } else return null; return bp; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload