所以,这就是我在变量中检索图像并将它们编码为基本64字符串所做的全部工作,以便将其发送到Web服务。但每次我在base 64 String中收到空值。其次,我无法访问OnActivityResult之外的位图图像。这是我的代码。
{
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#webservice。
注意:通过此代码,它在数据库中发送0X(空值)。
答案 0 :(得分:0)
在onActivityResult
中从相机中获取图像byte[] b = bund.getByteArray("data");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
String base64Str = encodeTobase64(bmp); //base64Str is the one that you want to send to the webservice.
public string encodeTobase64(Bitmap image)
{
String imageEncoded;
Bitmap immagex=image;
if ( immagex != null )
{ ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] b = baos.toByteArray();
imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
}
else
{
imageEncoded = "";
}
return imageEncoded ;
}
===编辑:
如果你想从imageview onclicklistener获取位图
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
答案 1 :(得分:0)
好的家伙在这里。这就是我解决自己问题的方法。我只是将捕获的图像保存在SD卡中的某个位置,然后使用相同的位置来检索相同的捕获图像,然后在我的代码中的任何位置使用它。
public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST=1111;
private ImageView imageVw;
public String encodedImage;
private static final String NAMESPACE="http://tempuri.org/";
private static final String URL="http://10.0.2.2:3694/WebService.asmx";
private static final String SOAP_ACTION="http://tempuri.org/insertData";
private static final String METHOD_NAME="insertData";
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
@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_PIC_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);
Bitmap bmp=retreivePic();
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 100, bos);
byte[] imageBytes = bos.toByteArray();
String encoded = Base64.encodeToString(imageBytes, Base64.DEFAULT);
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,encoded);
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("Sent");
//Toast.makeText(getBaseContext(), encoded, Toast.LENGTH_LONG).show();
//TextView txt7=(TextView)findViewById(R.id.txtMsg7);
//txt7.setText(encoded);
}
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
Bitmap thumb=(Bitmap)data.getExtras().get("data");
//img.setImageBitmap(thumb);
ByteArrayOutputStream bytes=new ByteArrayOutputStream();
thumb.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file=new File(Environment.getExternalStorageDirectory()+File.separator+"image.jpg");
try{
file.createNewFile();
FileOutputStream fo=new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
public Bitmap retreivePic()
{
File f=new File(Environment.getExternalStorageDirectory()+File.separator+"image.jpg");
Bitmap bmp=BitmapFactory.decodeFile(f.getAbsolutePath());
return bmp;
}
}