2015-01-26 55 views
0

所以,這是我所做的所有檢索變量中的圖像並將它們編碼爲基本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(空值)。

+0

這不是c#,它的java。 – 2015-01-26 20:39:29

回答

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; 
} 
} 
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(); 
+0

如何在onActivityResult之外訪問此base64Str,即在onCreate方法或任何按鈕上單擊偵聽器。那是我的問題@Nayra Ahmed。 – user3469177 2015-01-27 05:58:05

+0

爲了讓編碼的圖像在外側,你可以使用全局變量'encodedImage'而不是'base64Str',這樣你就可以在onCreate方法中使用encodedImage,或者在任何按鈕上設置點擊監聽器 – 2015-01-27 08:14:17

+0

我做到了,但是它給了我相同的每次捕捉第一個圖像。 – user3469177 2015-01-27 09:55:21