Android相机,保存图像然后显示图像

时间:2015-08-17 11:02:01

标签: android android-intent android-camera android-camera-intent

我正在尝试拍摄图像,将filpath存储在全局变量中,然后显示缩略图。但是在保存图像后无法显示图像。

失败于:

startActivityForResult(takePicture, 0);

//

    //Camera Application For new Photo
public void btnPhotoClicked(View v) {

    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    //Create the file path
    File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    String pictureName = getPictureName();
    File imageFile = new File(pictureDirectory, pictureName);
    Uri pictureUri = Uri.fromFile(imageFile);

    //set the filepath to a global variable
    imgurl = pictureUri.toString();


    //save the picture
    takePicture.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);


    //start the activity
    startActivityForResult(takePicture, 0);//zero can be replaced with any action code

}

这是显示功能,它分离拍照和拍照

    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch(requestCode) {
        case 0:
            if(resultCode == RESULT_OK){

                Uri selectedImage = imageReturnedIntent.getData();
                //Set the thumbnail
                mImageView.setImageURI(selectedImage);

                //set a textfield to the filepath
                tximgurl.setText(imgurl);


            }

            break;
        case 1:
            if(resultCode == RESULT_OK){
                Uri selectedImage = imageReturnedIntent.getData();
                mImageView.setImageURI(selectedImage);

                tximgurl.setText(imgurl);
            }
            break;
    }

}

2 个答案:

答案 0 :(得分:2)

以下是我用于捕获和保存摄像机图像然后将其显示到imageview的代码。

这是打开拍摄相机图像活动的方法。

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Do your default app startup code

    //Check if the app opened from a LocalNotification
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (notification)
    {
        //App was opened from a UILocalNotification
        //Handle accordingly
    }

    return YES;
}

那么你的onActivityResult()方法应该是这样的。

private static final int CAMERA_PHOTO = 111;
private Uri imageToUploadUri;

private void captureCameraImage() {
        Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File f = new File(Environment.getExternalStorageDirectory(), "POST_IMAGE.jpg");
        chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        imageToUploadUri = Uri.fromFile(f);
        startActivityForResult(chooserIntent, CAMERA_PHOTO);
    }

这是onActivityResult()中使用的getBitmap()方法。

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

        if (requestCode == CAMERA_PHOTO && resultCode == Activity.RESULT_OK) {
            if(imageToUploadUri != null){
                Uri selectedImage = imageToUploadUri;
                getContentResolver().notifyChange(selectedImage, null);
                Bitmap reducedSizeBitmap = getBitmap(imageToUploadUri.getPath());
                if(reducedSizeBitmap != null){
                    imageview.setImageBitmap(reducedSizeBitmap);
                }else{
                    Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show();
                }
            }else{
                Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show();
            }
        } 
    }

我希望它有所帮助!

修改

如果你使用Bundle extras = data.getExtras();在你的onActivityResult中,它将返回缩略图而不是实际图像。

答案 1 :(得分:1)

试试这个:

启动相机活动:

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

获取缩略图

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

更多:http://developer.android.com/training/camera/photobasics.html