本文介绍了如何做好上传图像排球库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个形象,我想这个图片上传到用排枪库我的web服务,问题是我正在寻找一个如何做到这一点,但仍然没有找到。
我发现了这一点,但不工作对我说:的
我怎样才能做到这一点?
我想这一点。
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,因此code,数据);
如果(要求code == RESULT_LOAD_IMAGE和放大器;&安培;结果code == RESULT_OK和放大器;&安培;!NULL =数据){
乌里selectedImage = data.getData();
的String [] filePathColumn = {MediaStore.Images.Media.DATA};
光标光标= getContentResolver()查询(selectedImage,
filePathColumn,NULL,NULL,NULL);
cursor.moveToFirst();
INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
fotoPerfil = cursor.getString(参数:columnIndex);
cursor.close();
ImageView的ImageView的=(ImageView的)findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.de codeFILE(picturePath));
}
}
UploadoImageToServer上传=新UploadoImageToServer();
ApplicationController的应用程序=新UsuarioDAO()。插入(usuario,upload.upload(fotoPerfil,EM [0]),新UsuarioAdapter(){
@覆盖
公共无效usuarioIsAdded(布尔结果){
Log.i(Adicionou:结果+);
progress.dismiss();
}
});
CustomVolleySingleton.getInstance(getApplicationContext())addToRequestQueue(应用程序)。
公共类UploadoImageToServer {
公共字符串上传(字符串形象,字符串nomeImagem){
点阵位图= BitmapFactory.de codeFILE(图像);
ByteArrayOutputStream流=新ByteArrayOutputStream();
bitmap.com preSS(Bitmap.Com pressFormat.PNG,90,流);
byte []的byte_arr = stream.toByteArray();
字符串image_str = Base64.en codeBytes(byte_arr);
返回image_str;
}
}
/ **添加一个用户,并上传您的照片(图片)* /
公共ApplicationController中插入(Usuario U,串uploadFile,最终UsuarioAdapter监听器){
布尔插入=假;
HashMap的<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
params.put(咔嗒,u.getNome());
params.put(电子邮件,u.getEmail());
params.put(senha,u.getSenha());
params.put(TIPO,usuarios);
params.put(ACAO,增加);
params.put(device_tipo,机器人);
params.put(设备,AndroidReturnId.getAndroidId());
params.put(UploadedFile的,uploadFile);
Log.i(URL:,urlPost.toString());
ApplicationController的APC =新的ApplicationController(Method.POST,
urlPost.toString(),
参数,可以
新Response.Listener<的JSONObject>(){
@覆盖
公共无效onResponse(JSONObject的OBJ){
尝试 {
如果(obj.getString(鳕鱼)。等于(999)){
listener.usuarioIsAdded(真正的);
}其他{
listener.usuarioIsAdded(假);
}
}赶上(JSONException E){
e.printStackTrace();
}
}
},
新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError为arg0){
Log.e(误差法:,插入UsuarioDAO:+ arg0.getLocalizedMessage());
}
});
返回APC;
}
解决方案
我倒没familier与凌空抽射,但给一个尝试以下code
// JSON请求
公共MySampleImageUpload(){
JSONRequestResponse mResponse =新
JSONRequestResponse(mContext);
捆绑PARMS =新包();
parms.putString(key_meail,rojesh@demo.com);
parms.setFile(key_url,image_path);
mResponse.getResponse(sample_upload_data_url,REQUEST_ code,这一点,
PARMS);
}
//在SetFile和放大器; GETRESPONSE code
包com.fartogram.utils;
进口的java.io.File;
进口org.json.JSONObject;
进口android.content.Context;
进口android.os.Bundle;
进口com.android.volley.Request;
进口com.android.volley.Response;
进口com.android.volley.VolleyError;
进口com.android.volley.examples.toolbox.MultipartRequest;
进口com.android.volley.examples.toolbox.MyVolley;
进口com.android.volley.toolbox.JsonObjectRequest;
公共类JSONRequestResponse {
公共JSONRequestResponse(上下文CNTX){
mContext = CNTX;
}
私人最终语境mContext;
私人诠释REQ code;
私人IParseListener听者;
私人布尔ISFILE = FALSE;
私人字符串FILE_PATH =,关键=;
公共无效GETRESPONSE(字符串URL,最终诠释请求code,
IParseListener mParseListener){
GETRESPONSE(URL,请求code,mParseListener,NULL);
}
公共无效GETRESPONSE(字符串URL,最终诠释请求code,
IParseListener mParseListener,捆绑PARAMS){
this.listner = mParseListener;
this.req code =请求code;
Response.Listener<的JSONObject> sListener =新Response.Listener<的JSONObject>(){
@覆盖
公共无效onResponse(JSONObject的响应){
如果(听者!= NULL){
listner.SuccessResponse(响应,REQ code);
}
}
};
Response.ErrorListener eListener =新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
如果(听者!= NULL){
listner.ErrorResponse(错误,REQ code);
}
}
};
如果(!ISFILE){
JsonObjectRequest jsObjRequest =新JsonObjectRequest(
Request.Method.GET,网址,空,sListener,
eListener);
MyVolley.getRequestQueue()加(jsObjRequest)。
} 其他 {
如果(FILE_PATH!= NULL){
文件MFILE =新的文件(FILE_PATH);
MultipartRequest multipartRequest =
新MultipartRequest(URL,eListener,sListener,钥匙,MFILE,则params);
MyVolley.getRequestQueue()加(multipartRequest)。
}
}
}
公共布尔ISFILE(){
返回ISFILE;
}
公共无效setFile(字符串参数,字符串路径){
如果(PATH =空&放大器;!&安培;!参数= NULL){
键=参数;
FILE_PATH =路径;
this.isFile = TRUE;
}
}
}
如果你的作品将其标记为正确的:)
I have an image and I want to upload this image to my web service using Volley library, the problem is I'm looking for a how to do it but still haven't found.
I found this, but doesn't work to me: http://develop-for-android.blogspot.com.br/2014/01/using-volley-in-your-application.html
How can I do this ?
I'm trying this.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
fotoPerfil = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
UploadoImageToServer upload = new UploadoImageToServer();
ApplicationController app = new UsuarioDAO().insert(usuario, upload.upload(fotoPerfil, em[0]), new UsuarioAdapter(){
@Override
public void usuarioIsAdded(Boolean result){
Log.i("Adicionou: ", result + "");
progress.dismiss();
}
});
CustomVolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(app);
public class UploadoImageToServer {
public String upload(String image,String nomeImagem){
Bitmap bitmap = BitmapFactory.decodeFile(image);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
return image_str;
}
}
/** add an user and upload your foto(image) */
public ApplicationController insert(Usuario u, String uploadFile, final UsuarioAdapter listener){
boolean insert = false;
HashMap<String, String> params = new HashMap<String, String>();
params.put("nome", u.getNome());
params.put("email", u.getEmail());
params.put("senha", u.getSenha());
params.put("tipo", "usuarios");
params.put("acao", "add");
params.put("device_tipo", "android");
params.put("device", AndroidReturnId.getAndroidId());
params.put("uploadedfile", uploadFile);
Log.i("URL: ", urlPost.toString());
ApplicationController apc = new ApplicationController(Method.POST,
urlPost.toString(),
params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject obj) {
try {
if(obj.getString("cod").equals("999")){
listener.usuarioIsAdded(true);
}else{
listener.usuarioIsAdded(false);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
Log.e("ERROR METHOD:", "insert in UsuarioDAO: " + arg0.getLocalizedMessage());
}
});
return apc;
}
解决方案
i am not much familier with volley, but give a try to the following code
//JSON Request
public MySampleImageUpload() {
JSONRequestResponse mResponse = new
JSONRequestResponse(mContext);
Bundle parms = new Bundle();
parms.putString("key_meail", "rojesh@demo.com");
parms.setFile("key_url", image_path);
mResponse.getResponse("sample_upload_data_url", REQUEST_CODE, this,
parms);
}
// In SetFile & getResponse code
package com.fartogram.utils;
import java.io.File;
import org.json.JSONObject;
import android.content.Context;
import android.os.Bundle;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.examples.toolbox.MultipartRequest;
import com.android.volley.examples.toolbox.MyVolley;
import com.android.volley.toolbox.JsonObjectRequest;
public class JSONRequestResponse {
public JSONRequestResponse(Context cntx) {
mContext = cntx;
}
private final Context mContext;
private int reqCode;
private IParseListener listner;
private boolean isFile = false;
private String file_path = "", key = "";
public void getResponse(String url, final int requestCode,
IParseListener mParseListener) {
getResponse(url, requestCode, mParseListener, null);
}
public void getResponse(String url, final int requestCode,
IParseListener mParseListener, Bundle params) {
this.listner = mParseListener;
this.reqCode = requestCode;
Response.Listener<JSONObject> sListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (listner != null) {
listner.SuccessResponse(response, reqCode);
}
}
};
Response.ErrorListener eListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (listner != null) {
listner.ErrorResponse(error, reqCode);
}
}
};
if (!isFile) {
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.GET, url, null, sListener,
eListener);
MyVolley.getRequestQueue().add(jsObjRequest);
} else {
if (file_path != null) {
File mFile = new File(file_path);
MultipartRequest multipartRequest =
new MultipartRequest(url,eListener, sListener, key, mFile, params);
MyVolley.getRequestQueue().add(multipartRequest);
}
}
}
public boolean isFile() {
return isFile;
}
public void setFile(String param, String path) {
if (path != null && param != null) {
key = param;
file_path = path;
this.isFile = true;
}
}
}
If it works for you mark it as right :)
这篇关于如何做好上传图像排球库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!