0

我已经使用内置图像捕获活动创建了拍照活动,但仍然无法将图片存储在 sdcard 中并查看捕获的图像。Intent 已启动,我可以拍照但当我单击确定(保存)时,什么也没有发生。以下是我的活动:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picturelayout);
    imageForUpload = (ImageView) findViewById(R.id.trackMePicture);
    btnBack = (Button) findViewById(R.id.btnBack);
    intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(this));
    startActivityForResult(intent, TAKE_PHOTO_CODE);
}

/**
 * @return
 */
private Uri getImageUri(Context context) {
    // TODO Auto-generated method stub

    File file =newFile(Environment.getExternalStorageDirectory(),context.getPackageName());
    if(!file.exists())
        file.mkdir();
    File newFile=new File(file,new Date().toString()+".jpg");
    Uri imagePath=Uri.fromFile(newFile);
    return imagePath;

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==TAKE_PHOTO_CODE ){
        if(resultCode==-1){

            Toast.makeText(getApplicationContext(), "Result code : "+resultCode, Toast.LENGTH_LONG).show();
            //Uri imagePath=getImageUri();
            Bitmap b;
            try {
                b = Media.getBitmap(getContentResolver(), getImageUri(this));
                imageForUpload.setImageBitmap(b);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }else{
            Toast.makeText(getApplicationContext(), "Result code : "+resultCode, Toast.LENGTH_LONG).show();
        }
    }
    else{
        Toast.makeText(getApplicationContext(), "Request  code : "+requestCode, Toast.LENGTH_LONG).show();
    }
}
4

3 回答 3

0

您的代码中没有onActivityResult()将位图存储b在文件中的代码。您可以查看我的 Zwitscher 应用程序中的PicHelper.storeBitmap()方法,了解如何执行此操作。

于 2012-10-03T11:37:21.697 回答
0

这是我的做法(用于拍照或浏览媒体目录),希望对您有所帮助:

static int SELECT_IMAGE = 2000;
static int CAMERA_REQUEST = 2001; 

public void ProfilePictureDialog()
{
    builder = new AlertDialog.Builder(this);
    builder.setCancelable(false);
    builder.setMessage("").setPositiveButton("Browse images", new DialogInterface.OnClickListener()
    {  
        @Override  
        public void onClick(DialogInterface dialog, int which)
        {  
            Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);       
            startActivityForResult(gallery, SELECT_IMAGE); 

        }}).setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {  
        @Override  
        public void onClick(DialogInterface dialog, int which)
        {  

        }}).setNeutralButton("Take picture with camera", new DialogInterface.OnClickListener(){  
        @Override  
        public void onClick(DialogInterface dialog, int which)
        {  
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }});
    builder.show();   
}





protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(resultCode == RESULT_OK)
    {
        if (requestCode == CAMERA_REQUEST)
        {  
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            ImageView profilePicture = (ImageView) findViewById(R.id.profilepic);

            photo = Bitmap.createScaledBitmap(photo, profilePicture.getWidth(), profilePicture.getHeight(), true);
            profilePicture.setImageBitmap(photo);
        }
        else if(requestCode == SELECT_IMAGE)
        {
            Uri selectedImagePath = data.getData();

            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImagePath, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

            Bitmap photo = BitmapFactory.decodeFile(filePath);

            ImageView profilePicture = (ImageView) findViewById(R.id.profilepic);

            photo = Bitmap.createScaledBitmap(photo, profilePicture.getWidth(), profilePicture.getHeight(), true);
            profilePicture.setImageBitmap(photo);
        }
    }
} 
于 2012-10-03T12:01:40.570 回答
0

尝试这样做:

final Bundle extras = data.getExtras(); 
if (extras != null) {
    Bitmap b = extras.getParcelable("data");
    imageForUpload.setImageBitmap(b);

}
于 2012-10-03T11:40:11.750 回答