所以,这是我所做的所有检索变量中的图像并将它们编码为基本64字符串,以便它可以发送到web服务。但每次我收到基础64字符串中的空值。其次,我无法访问OnActivityResult外部的位图图像。这是我的代码。无法检索onCreate方法内的捕获图像
{
private static final int CAMERA_REQUEST=1888;
private ImageView imageVw;
public String encodedImage="";
public String path;
private static final String NAMESPACE="http://tempuri.org/";
private static final String URL="http://10.0.2.2:2278/newWebService/WebService.asmx";
private static final String SOAP_ACTION="http://tempuri.org/insertData";
private static final String METHOD_NAME="insertData";
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);}
this.imageVw=(ImageView)this.findViewById(R.id.imgv1);
Button photoButton=(Button)this.findViewById(R.id.btnClick);
Button btn=(Button)findViewById(R.id.btnClick2);
photoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
EditText ed1=(EditText)findViewById(R.id.etName);
EditText ed2=(EditText)findViewById(R.id.etComplnt);
PropertyInfo pi=new PropertyInfo();
pi.type=PropertyInfo.STRING_CLASS;
pi.name="name";
PropertyInfo pi2=new PropertyInfo();
pi2.type=pi2.STRING_CLASS;
pi2.name="complnt";
PropertyInfo pi3=new PropertyInfo();
pi3.type=pi3.STRING_CLASS;
pi3.name="img";
request.addProperty(pi,ed1.getText().toString());
request.addProperty(pi2,ed2.getText().toString());
request.addProperty(pi3,loadImageFromStorage(path));
SoapSerializationEnvelope env=new SoapSerializationEnvelope(SoapEnvelope.VER11);
env.dotNet=true;
env.setOutputSoapObject(request);
HttpTransportSE htse=new HttpTransportSE(URL);
try{
htse.debug=true;
htse.call(SOAP_ACTION, env);
Object response=env.getResponse();
TextView txt2=(TextView)findViewById(R.id.txtMsg3);
txt2.setText(response.toString());
Toast.makeText(getBaseContext(), encodedImage, Toast.LENGTH_LONG).show();
TextView txt7=(TextView)findViewById(R.id.txtMsg7);
txt7.setText(encodedImage);
}
catch(Exception exc)
{
TextView txt6=(TextView)findViewById(R.id.txtMsg6);
txt6.setText(exc.toString());
System.out.println("Dump : "+htse.responseDump);
}
}
});
// String strBase64=Base64.encodeToString(returnPic(req, res, data), flags);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
returnPic(requestCode, resultCode, data);
}
@SuppressLint("NewApi")
public void returnPic(int req,int res,Intent data)
{
byte[] bits = null;
if(req==CAMERA_REQUEST && res==RESULT_OK)
{
Uri selectedImageUri = data.getData();
imageVw.setImageURI(selectedImageUri);
String imagepath = getPath(selectedImageUri);
Bitmap bitmap=BitmapFactory.decodeFile(imagepath);
/**ByteArrayOutputStream bos = new ByteArrayOutputStream();
BitmapDrawable drawable=((BitmapDrawable)imageVw.getDrawable());
Bitmap bitmap=drawable.getBitmap();**/
path= saveToInternalStorage(bitmap);
//ByteArrayOutputStream bytes=new ByteArrayOutputStream();
//bitmap.compress(CompressFormat.JPEG, 75, bytes);
//byte[] imageBytes = bytes.toByteArray();
//encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
// FileOutputStream fos=new FileOutputStream(getPath(selectedImageUri));
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public String saveToInternalStorage(Bitmap bitmapImage)
{
ContextWrapper cw=new ContextWrapper(getApplicationContext());
File directory=cw.getDir("imageDir", Context.MODE_PRIVATE);
File myPath=new File(directory,"profile.jpg");
FileOutputStream fos=null;
try{
fos=new FileOutputStream(myPath);
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}
catch(Exception exc)
{
exc.printStackTrace();
}
return directory.getAbsolutePath();
}
private String loadImageFromStorage(String path)
{
String encoder = null;
try{
File f=new File(path,"profile.jpg");
Bitmap b=BitmapFactory.decodeStream(new FileInputStream(f));
ByteArrayOutputStream bytes=new ByteArrayOutputStream();
b.compress(CompressFormat.JPEG, 75, bytes);
byte[] imageBytes = bytes.toByteArray();
encoder = Base64.encodeToString(imageBytes, Base64.DEFAULT);
}
catch(Exception exc){
exc.printStackTrace();
}
return encoder;
}
}
任何帮助表示赞赏,因为我一直被困在这件事上大约一周。我想要做的只是捕获图像并使用SOAP将其发送到C#web服务。
注意:通过此代码,它将在数据库中发送0X(空值)。
这不是c#,它的java。 – 2015-01-26 20:39:29