我正在尝试将图片上传到服务器,但我没有成功 我的错误在哪里?
如何发送图片属性?
位图? byte []数组?图像路径?
我不知道
这是我的代码 请帮帮我。
try
{
Uri uri=data.getData();
String[]projection={MediaStore.Images.Media.DATA};
Cursor cursor=getContentResolver().query(uri, projection, null,null,null);
cursor.moveToFirst();
String imagepath=cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
Bitmap yourSelectedImage=BitmapHelper.getBitmapFromPath(imagepath);
Bitmap bm = BitmapFactory.decodeFile(imagepath);
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 3;
bm = BitmapFactory.decodeFile(imagepath,options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 50, stream);
byte[] byte_arr = stream.toByteArray();
final String encodedString = Base64.encodeToString(byte_arr, 0);
File f = new File(imagepath);
imageName = f.getName();
stringToConvert = yourSelectedImage.toString();
theByteArray = stringToConvert.getBytes();
convertimage=BitmapHelper.getByteStringFromBitmap(yourSelectedImage);
new ImageGalleryBigPopup(ImageGalleryActivity.this, yourSelectedImage)
{
@SuppressLint("NewApi")
protected void OnSave() throws JSONException {
jArr = new JSONArray();
jsonObject.put("FileName", imageName);
jsonObject.put("FileContent", encodedString);
jsonObject.put("UploadStatus", upstat);
jArr.put(jsonObject);
for(int i = 0; i < jArr.length(); i++)
{
//images.add(new ImageGalleryItem(jArr.getJSONObject(i)));
saveImage(jArr.getJSONObject(i));
}
};
};
}
catch(Exception e)
{
Log.d("GALLERY", e.getMessage());
}
}
这是我的saveImage()函数
private void saveImage( JSONObject jsonObject2) throws JSONException
{
ImageGalleryItem galleryItem=new ImageGalleryItem(jsonObject2);
operationType = "0";
operationId = "45";
showLoadingPopup();
//fileName = Converter.encodeUrl(fileName);
String url = String.format(UrlConfig.IMAGE_SAVE, operationType, operationId);
new ConnectionHelper(ImageGalleryActivity.this,url,galleryItem.toString()) {
@Override
protected void OnSuccess(String response) {
// TODO Auto-generated method stub
hideLoadingPopup();
Log.e("ON succes", response);
//Toast.makeText(getApplicationContext(),"Resim Eklenmistir", Toast.LENGTH_LONG).show();
getList();
}
@Override
protected void OnFail(Throwable error, String message) {
// TODO Auto-generated method stub
hideLoadingPopup();
Log.e("Throwable", error.toString());
Log.e("message", message);
}
@Override
protected void OnException(Exception exception) {
// TODO Auto-generated method stub
hideLoadingPopup();
Log.e("Exception", exception.toString());
}
};
}
答案 0 :(得分:-1)
使用像Ion by Koush
这样的库更简单你可以这样使用它:
Ion.with(getBaseContext())
.load("http://your_destination_url")
.uploadProgressHandler(new ProgressCallback() {
@Override
public void onProgress(long uploaded, long total) {
// Your progress behaviour
}
})
.setMultipartFile("name_file", "image/jpeg", new File(YourFile))
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
// To do on complete upload
}
});