我正在从Android手机摄像头拍照并将其放在imageview上。现在我想拍摄imageview的src,以便我可以将它上传到服务器。我如何拍摄imageview的src?
这是我的代码
public class CameraActivity extends Activity implements View.OnClickListener {
ImageView iv;
Button bCapture, bSetWall;
Intent i;
int CameraResult = 0;
Bitmap bmp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialize();
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmp = BitmapFactory.decodeStream(is);
}
private void initialize() {
iv = (ImageView)findViewById(R.id.ivCamera);
bCapture = (Button)findViewById(R.id.bCapture);
bSetWall = (Button)findViewById(R.id.bSetWall);
bCapture.setOnClickListener(this);
bSetWall.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.bCapture:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CameraResult);
break;
case R.id.bSetWall:
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
//String v = iv.getTag().toString();
//Toast.makeText(getApplicationContext(), v, Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:1)
将位图转换为字节数组并将其发送到服务器
Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
答案 1 :(得分:1)