如何在将图像上传到服务器之前调整其大小。有人能帮助我吗?
这是我的代码:
package com.projectappnew.kmitl_1;
import java.io.File;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Update_Image1 extends Activity {
private static final int SELECT_FILE1 = 1;
String selectedPath1 = "NONE";
TextView tv1;
TextView res;
ProgressDialog progressDialog;
Button buttonSelecte1;
Button buttonUpload;
HttpEntity resEntity;
////////////////
TextView tvView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_image1);
tvView1 = (TextView) findViewById(R.id.tvView1);
Intent intent = getIntent();
String sProductID = intent.getStringExtra("s_ProductID");
tvView1.setText(" " + sProductID );
tv1 = (TextView)findViewById(R.id.tv1);
res = (TextView)findViewById(R.id.res);
tv1.setText(tv1.getText() + selectedPath1);
buttonSelecte1 = (Button)findViewById(R.id.buttonSelecte1);
buttonUpload = (Button)findViewById(R.id.buttonUpload);
buttonSelecte1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openGallery(SELECT_FILE1);
}
});
buttonUpload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(!(selectedPath1.trim().equalsIgnoreCase("NONE"))){
`progressDialog = ProgressDialog.show(Update_Image1.this, "", "Uploading files to server.....", false)`;
Thread thread=new Thread(new Runnable(){
public void run(){
doFileUpload();
runOnUiThread(new Runnable(){
public void run() {
if(progressDialog.isShowing())
progressDialog.dismiss();
}
});
}
});
thread.start();
}else{
Toast.makeText(getApplicationContext(),"Please select two files to upload.", Toast.LENGTH_SHORT).show();
}
}
});
}
public void openGallery(int req_code){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select file to upload "), req_code);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//Uri selectedImageUri = data.getData();
Uri selectedImageUri = data.getData();
if (requestCode == SELECT_FILE1)
{
selectedPath1 = getPath(selectedImageUri);
System.out.println("selectedPath1 : " + selectedPath1);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
private void doFileUpload(){
File file1 = new File(selectedPath1);
final Intent intent = getIntent();
String sProductID = intent.getStringExtra("s_ProductID");
String urlString = "http://projectappnew.com/kmitl/sid/update_image1.php?ProductID="+ sProductID;
try
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
FileBody bin1 = new FileBody(file1);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("uploadedfile1", bin1);
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
final String response_str = EntityUtils.toString(resEntity);
if (resEntity != null) {
Log.i("RESPONSE",response_str);
runOnUiThread(new Runnable(){
public void run() {
try {
res.setTextColor(Color.GREEN);
res.setText("n Response from server : n " + response_str);
Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
Intent newActivity = new Intent(Update_Image1.this,Show_Category.class);
final Intent intent = getIntent();
String strProductID = intent.getStringExtra("ProductID");
newActivity.putExtra("s_ProductID", strProductID);
startActivity(newActivity);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
catch (Exception ex){
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
}
}
有人可以告诉你要添加什么以及在哪里?