开发者

problem displaying image after capturing through camera?

开发者 https://www.devze.com 2023-01-08 08:19 出处:网络
I am using following code to capture image through camera and then displaying it. The problem , I am facing is when i take picture and press done button it does not display that taken image in my act

I am using following code to capture image through camera and then displaying it.

The problem , I am facing is when i take picture and press done button it does not display that taken image in my activity.

It just shows blank image field.

Any one guide me what mistake am i doing? might be path related issue but I am not sure.

protected Button _button;
    protected ImageView _image;
    protected TextView _field;
    protected String _path;
    protected boolean _taken;

    protected static final String PHOTO_TAKEN   = "photo_taken";

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        _image = ( ImageView ) findViewById( R.id.image );
        _field = ( TextView ) findViewById( R.id.field );
        _button = ( Button ) findViewById( R.id.button );
        _button.setOnClickListener( new ButtonClickHandler() );

        _path = Environment.getExternalStorageDirectory() + "/images/media/make_machine_example.png";

      //media/external/images/media/2


    }

    public class ButtonClickHandler implements View.OnClickListener 
    {
        public void onClick( View view ){
            Log.i("MakeMachine", "ButtonClickHandler.onClick()" );
            startCameraActivity();
        }
    }

    protected void startCameraActivity()
    {
        Log.i("MakeMachine", "startCameraActivity()" );
        File file = new File( _path );
        Uri outputFileUri = Uri.fromFile( file );

        Intent intent = new Intent(开发者_如何学Pythonandroid.provider.MediaStore.ACTION_IMAGE_CAPTURE );
        intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

        startActivityForResult( intent,0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {   
        Log.i( "MakeMachine", "resultCode: " + resultCode );
        switch( resultCode )
        {
            case 0:
                Log.i( "MakeMachine", "User cancelled" );
                break;

            case -1:
                onPhotoTaken();
                break;
        }
    }

    protected void onPhotoTaken()
    {
        Log.i( "MakeMachine", "onPhotoTaken" );

        _taken = true;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;


        Bitmap bitmap = BitmapFactory.decodeFile( _path, options );

        _image.setImageBitmap(bitmap);

        _field.setVisibility( View.GONE );
    }


Make sure there is a folder structure on your device that matches the path your are sending in the intent.
http://labs.makemachine.net/2010/03/simple-android-photo-capture/comment-page-1/#comment-143

You will need to manually create images/media folders on your device. They will not be created automagically by attempting to save a file there.


this is what i was looking for

directory structure i am getting through my captured Uri data and now i am able to display image.

no need to use hard coded image path.

public String getRealPathFromURI(Uri contentUri) {   

    // can post image   
    String [] proj={MediaStore.Images.Media.DATA};   
    Cursor cursor = managedQuery( contentUri,   
            proj, // Which columns to return   
            null,       // WHERE clause; which rows to return (all rows)   
            null,       // WHERE clause selection arguments (none)   
            null); // Order-by clause (ascending by name)   
    int column_index = 
    cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);   
    cursor.moveToFirst();   

    return cursor.getString(column_index);   
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号