如何在android中获取imageview的src

时间:2012-06-11 03:47:42

标签: java android

我正在从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();
    }
}


}

2 个答案:

答案 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)

可以在SD卡或手机位置存储中保存而不保存上传图像视图。如果要在从相机拍摄照片后保存图像,则需要在位图中转换图像视图并将其保存在SD卡中。再次,您需要转换位图中的SD卡图像以上传它的服务器。

Link1

Link

感谢