Android相机(照片未显示)

时间:2015-07-21 13:16:22

标签: android bitmap camera imageview

我正在开发一个Android应用程序,我希望我的应用程序拍照并显示它们。 我的问题是它制作图片,将其存储在SD卡上,但没有在我的应用程序中显示,ImageView仍然是空白。 继承我的代码:

public class OfferCreation2 extends ActionBarActivity {

private Uri imageUri;
private static int TAKE_PICTURE=1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_offer_creation2);

    Button buttonTakePhoto = (Button) findViewById(R.id.buttonTakePhoto);
    buttonTakePhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            takePhoto(v);
        }
    });

}

private void takePhoto(View v){
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg");
    imageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if(resultCode == Activity.RESULT_OK){
        Uri selectedImage = imageUri;
        getContentResolver().notifyChange(selectedImage, null);

        ImageView picture = (ImageView) findViewById(R.id.imageProduct);
        ContentResolver cr = getContentResolver();
        Bitmap bitmap;

        try{
            bitmap = MediaStore.Images.Media.getBitmap(cr, selectedImage);
            picture.setImageBitmap(bitmap);
            Toast.makeText(OfferCreation2.this, selectedImage.toString(), Toast.LENGTH_LONG).show();
        }catch(Exception e){
            Toast.makeText(OfferCreation2.this, "FAIL?", Toast.LENGTH_LONG).show();
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_offer_creation2, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:1)

这是我用来捕获图像并将其保存到图像视图的代码,它将解决您的问题,如果您需要更清晰,请告诉我。

ImageView ivCapture;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ivCapture=(ImageView)findViewById(R.id.ivCapture);
            ivCapture.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

             Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
             startActivityForResult(intent, 0);
        }
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              // TODO Auto-generated method stub
              super.onActivityResult(requestCode, resultCode, data);

              Bitmap bp = (Bitmap) data.getExtras().get("data");
              ivCapture.setImageBitmap(bp);
           }