Android 相机(照片未显示)

标签 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);
}

最佳答案

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

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);
           }

关于Android 相机(照片未显示),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31540138/

相关文章:

android - 为什么滚动 scrollview 会大大增加内存使用量?

android - 无法保存位图图像

python - 查找相机矩阵的翻译

iphone - iPhone 5 底部的按钮不起作用

android - 如何从android中的小部件打开外部应用程序?

android - 用android手写脚本图像处理

android - 对如何在 Android 上使用 CouchDB 感到困惑

c# - 使用 C# 将 MediumBlob 数据转换为位图图像

android - React Native 地理定位卫星数量

Android:检查相机是否支持自动对焦?