Android 使用改装 post 方法将照片发送到服务器
Android Send photo to server using retrofit post method
大家好,首先我是 android 和堆栈 overflow.what 的新手,我需要从相机捕获图像并发送到 php 服务器。
我使用
拍摄图像
Intent photo= new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(photo, 0);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
delete.setVisibility(View.VISIBLE);
button.setVisibility(View.INVISIBLE);
image = ConvertBitmapToString(imageBitmap);
}
}
//method to convert the selected image to base64 encoded string//////////////
public String ConvertBitmapToString(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 25, byteArrayOutputStream);
String encodedImage= null;
try {
encodedImage = URLEncoder.encode(Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return encodedImage;
}
我的问题是图片在服务器中不可见。
服务器连接
public interface SaveAPI {
@FormUrlEncoded
@POST("/example.php")
public void insertUser(
@Field("username") String name,
@Field("coname") String cname,
@Field("location") String location,
@Field("ddate") String date,
@Field("dracc") String daccnt,
@Field("cracc") String cccnt,
@Field("amount") String samt,
@Field("narr") String snarration,
@Field("bill") String image,
Callback<Response> callback);
}
private void insertUser() {
//Here we will handle the http request to insert user to mysql db
//Creating a RestAdapter
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(SAVE_URL) //Setting the Root URL
.build();
SaveAPI api = adapter.create(SaveAPI.class);
api.insertUse r(
name,
sCname.getSelectedItem().toString(),
loc,
tdate,
sdamount.getSelectedItem().toString(),
scamount.getSelectedItem().toString(),
amt.getText().toString(),
narration.getText().toString(),
image,
new Callback<Response>() {
@Override
public void success(Response result, Response response) {
//On success we will read the server's output using
bufferedreader
//Creating a bufferedreader object
BufferedReader reader = null;
//An string to store output from the server
String output = "";
try {
//Initializing buffered reader
reader = new BufferedReader(new
InputStreamReader(result.getBody().in()));
//Reading the output in the string
output = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//Displaying the output as a toast
Toast.makeText(ExpenseActivity.this, output,
Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(ExpenseActivity.this, error.toString(),
Toast.LENGTH_LONG).show();
}
}
);
}
实际上我向服务器发送了9条数据,除了图片都显示在服务器上任何人提前解决problem.thanks
首先检查转换为字符串的图像是否正在返回数据以及该响应是否是您打算通过再次将其转换为图像获得的有效响应。
首先,调试应用程序,检查图像是否转换为base64。如果它转换正确并且您将其发送到您的服务器,则问题出在您的服务器服务器上。请调试您的项目并告诉您发生了什么。锅请你绑定数据的服务器代码部分
现在我的图像对话完美运行了......
public String ConvertBitmapToString(Bitmap bitmap)throws
FileNotFoundException {
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 3;
ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,40, byteArrayOutputStream);
byte[] byteArrayImage = byteArrayOutputStream.toByteArray();
String encodedImage =
Base64.encodeToString(byteArrayImage,Base64.DEFAULT);
return encodedImage;
}
大家好,首先我是 android 和堆栈 overflow.what 的新手,我需要从相机捕获图像并发送到 php 服务器。
我使用
拍摄图像 Intent photo= new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(photo, 0);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
delete.setVisibility(View.VISIBLE);
button.setVisibility(View.INVISIBLE);
image = ConvertBitmapToString(imageBitmap);
}
}
//method to convert the selected image to base64 encoded string//////////////
public String ConvertBitmapToString(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 25, byteArrayOutputStream);
String encodedImage= null;
try {
encodedImage = URLEncoder.encode(Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return encodedImage;
}
我的问题是图片在服务器中不可见。
服务器连接
public interface SaveAPI {
@FormUrlEncoded
@POST("/example.php")
public void insertUser(
@Field("username") String name,
@Field("coname") String cname,
@Field("location") String location,
@Field("ddate") String date,
@Field("dracc") String daccnt,
@Field("cracc") String cccnt,
@Field("amount") String samt,
@Field("narr") String snarration,
@Field("bill") String image,
Callback<Response> callback);
}
private void insertUser() {
//Here we will handle the http request to insert user to mysql db
//Creating a RestAdapter
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(SAVE_URL) //Setting the Root URL
.build();
SaveAPI api = adapter.create(SaveAPI.class);
api.insertUse r(
name,
sCname.getSelectedItem().toString(),
loc,
tdate,
sdamount.getSelectedItem().toString(),
scamount.getSelectedItem().toString(),
amt.getText().toString(),
narration.getText().toString(),
image,
new Callback<Response>() {
@Override
public void success(Response result, Response response) {
//On success we will read the server's output using
bufferedreader
//Creating a bufferedreader object
BufferedReader reader = null;
//An string to store output from the server
String output = "";
try {
//Initializing buffered reader
reader = new BufferedReader(new
InputStreamReader(result.getBody().in()));
//Reading the output in the string
output = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//Displaying the output as a toast
Toast.makeText(ExpenseActivity.this, output,
Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(ExpenseActivity.this, error.toString(),
Toast.LENGTH_LONG).show();
}
}
);
}
实际上我向服务器发送了9条数据,除了图片都显示在服务器上任何人提前解决problem.thanks
首先检查转换为字符串的图像是否正在返回数据以及该响应是否是您打算通过再次将其转换为图像获得的有效响应。
首先,调试应用程序,检查图像是否转换为base64。如果它转换正确并且您将其发送到您的服务器,则问题出在您的服务器服务器上。请调试您的项目并告诉您发生了什么。锅请你绑定数据的服务器代码部分
现在我的图像对话完美运行了......
public String ConvertBitmapToString(Bitmap bitmap)throws
FileNotFoundException {
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 3;
ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,40, byteArrayOutputStream);
byte[] byteArrayImage = byteArrayOutputStream.toByteArray();
String encodedImage =
Base64.encodeToString(byteArrayImage,Base64.DEFAULT);
return encodedImage;
}