0

組み込みのイメージ キャプチャ アクティビティを使用して写真を撮るためのアクティビティを作成しましたが、SD カードに写真を保存したり、キャプチャした画像を表示したりすることはまだできません。インテントが開始され、写真を撮ることができますが、[OK](保存)何も起こりません。以下は私の活動です:

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

これを試してください:

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

}
于 2012-10-03T11:40:11.750 に答える
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 に答える