最新更新..我有上传图片的代码..但我不知道这个代码要做什么..?来自http://monstercoda.wordpress.com/2012/04/15/android-image-upload-tutorial-part-i/的源代码
代码如下:
public class MainActivity extends Activity {
Uri currImageURI;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button gallery_btn = (Button)findViewById(R.id.gallerybtn);
gallery_btn.setOnClickListener(new OnClickListener(){
public void onClick(View view){
//to open up a gallery browser
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture.."),1);
(getRealPathFromURI(currImageURI);
}
});
}
// To handle when an image is selected from the browser
public void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
// currImageURI is the global variable I’m using to hold the content:
currImageURI = data.getData();
String s = ("Current image Path is ----->" + getRealPathFromURI(currImageURI));
TextView tv_path = (TextView) findViewById(R.id.path);
tv_path.setText(getRealPathFromURI(currImageURI));
}
HttpUploader uploader = new HttpUploader();
try {
uploader.execute(getRealPathFromURI(currImageURI));
}
}
//Convert the image URI to the direct file system path of the image file
public String getRealPathFromURI( Uri contentUri) {
String [] proj={MediaStore.Images.Media.DATA};
android.database.Cursor cursor = managedQuery(contentUri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String c = cursor.getString(column_index);
return c;
}
}
我的 httpUploader 就像他们提供的一样简单:
public class HttpUploader extends AsyncTask<String, Void, String> {
protected String doInBackground(String... path) {
String outPut = null;
for (String sdPath : path) {
Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
//Resize the image
double width = bitmapOrg.getWidth();
double height = bitmapOrg.getHeight();
double ratio = 400/width;
int newheight = (int)(ratio*height);
System.out.println("———-width" + width);
System.out.println("———-height" + height);
bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true);
//Here you can define .PNG as well
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao);
byte[] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
String k = "uploading image now ——–" + ba1 ;
Log.e("k contains", k);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//store path for image name and id for each profile id.
nameValuePairs.add(new BasicNameValuePair("image", ba1));
nameValuePairs.add(new BasicNameValuePair("path", sdPath));
nameValuePairs.add(new BasicNameValuePair("id", id));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("192.168.1.1/upload.php"); // which request for $_POST['image'];
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// print responce
outPut = EntityUtils.toString(entity);
Log.i("GET RESPONSE—-", outPut);
//is = entity.getContent();
Log.e("log_tag ******", "good connection");
bitmapOrg.recycle();
} catch (Exception e) {
Log.e("log_tag ******", "Error in http connection " + e.toString());
}
}
return outPut;
}
}
PHP文件保存到数据库:
if (isset($base)) {
$base = $_REQUEST["image"];
$filepath = $_REQUEST["path"];
$id = $_REQUEST["id"];
$image_name = "/image/".$filepath;
// base64 encoded utf-8 string
$binary = base64_decode($base);
// binary, utf-8 bytes
header("Content-Type: bitmap; charset=utf-8");
$file = fopen($image_name, "wb");
fwrite($file, $binary);
fclose($file);
$result = "UPDATE profile SET image = '$image_name' WHERE RID = '$id'");
if(mysql_affected_rows > 0){
echo json_encode ("success!");
}else{
echo json_encode ("failed!");
}
} else {
die("No POST");
}
?>
或者如果代码不合适......可以为初学者提供任何指导吗?那里有太多版本的代码..我可以稍微理解这一点,因为它比其他版本更简单。