Php uploading image to server in android -
im using php script allows choose image gallery , upload online web hosting service. image gets uploaded, .jpg file uploaded empty , named 0.jpg , size shown 0 bytes. when try open image shows
"the image http://testimage.site88.net/pic/0.jpg cannot displayed because contains errors"
what possible reason this? php code follows
<?php $name = $_post['name']; //image name $image = $_post['image']; //image in string format //decode image $decodedimage = base64_decode($image); //upload image file_put_contents("pic/".$name.".jpg", $decodedimage);
the android code is:
import android.app.activity; import android.content.intent; import android.graphics.bitmap; import android.graphics.drawable.bitmapdrawable; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.provider.mediastore; import android.util.base64; import android.util.log; import android.view.view; import android.widget.button; import android.widget.imageview; import android.widget.toast; import java.io.bytearrayoutputstream; import java.io.unsupportedencodingexception; import java.net.urlencoder; import java.util.hashmap; import java.util.map; public class mainactivity extends activity { //define global views variable public imageview imageview; public button selectimage, uploadimage; public string server = "http://testimage.site88.net/saveimage.php", timestamp; private static final string tag = mainactivity.class.getsimplename(); private static final int result_select_image = 1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //instantiate view imageview = (imageview) findviewbyid(r.id.imageview); selectimage = (button) findviewbyid(r.id.selectimage); uploadimage = (button) findviewbyid(r.id.uploadimage); //when selectimage button pressed selectimage.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //call function select image album selectimage(); } }); //when uploadimage button pressed uploadimage.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //get image in bitmap format bitmap image = ((bitmapdrawable) imageview.getdrawable()).getbitmap(); //execute async task , upload image server new upload(image,"img_"+timestamp).execute(); } }); } //function select image private void selectimage(){ //open album select image intent gallaryintent = new intent(intent.action_pick,mediastore.images.media.external_content_uri); startactivityforresult(gallaryintent, result_select_image); } /* * function called when pick image album * */ @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == result_select_image && resultcode == result_ok && data != null){ //set selected image image variable uri image = data.getdata(); imageview.setimageuri(image); //get current timestamp , strore in time variable long tslong = system.currenttimemillis() / 1000; timestamp = tslong.tostring(); toast.maketext(getapplicationcontext(),timestamp,toast.length_short).show(); } } private string hashmaptourl(hashmap<string, string> params) throws unsupportedencodingexception { stringbuilder result = new stringbuilder(); boolean first = true; for(map.entry<string, string> entry : params.entryset()){ if (first) first = false; else result.append("&"); result.append(urlencoder.encode(entry.getkey(), "utf-8")); result.append("="); result.append(urlencoder.encode(entry.getvalue(), "utf-8")); } return result.tostring(); } //async task upload image private class upload extends asynctask<void,void,string>{ private bitmap image; private string name; public upload(bitmap image,string name){ this.image = image; this.name = name; } @override protected string doinbackground(void... params) { bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream(); //compress image jpg format image.compress(bitmap.compressformat.jpeg,100,bytearrayoutputstream); /* * encode image base64 can picked saveimage.php file * */ string encodeimage = base64.encodetostring(bytearrayoutputstream.tobytearray(),base64.default); //generate hashmap store encodedimage , name hashmap<string,string> detail = new hashmap<>(); detail.put("name", name); detail.put("image", encodeimage); try{ //convert hashmap encodedurl send php file string datatosend = hashmaptourl(detail); //make http request , send data saveimage.php file string response = request.post(server,datatosend); //return response return response; }catch (exception e){ e.printstacktrace(); log.e(tag,"error "+e); return null; } } @override protected void onpostexecute(string s) { //show image uploaded toast.maketext(getapplicationcontext(),"image uploaded",toast.length_short).show(); } } }
thanks in advance.
i think have fix php file. edited in question, didn't make sense. use what's there now.
if need more help, please give output of
error_log(print_r($_post, 1));
from php logs.
Comments
Post a Comment