代码之家  ›  专栏  ›  技术社区  ›  UMAR-MOBITSOLUTIONS

通过相机拍摄后显示图像时出现问题?

  •  0
  • UMAR-MOBITSOLUTIONS  · 技术社区  · 15 年前

    我面临的问题是当我拍照和按 完成 按钮它不显示我的活动中拍摄的图像。

    它只显示空白图像字段。

    有人告诉我我犯了什么错吗?可能是路径相关的问题,但我不确定。

    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(android.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 );
        }
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   JeremyFromEarth    15 年前

    确保您的设备上有一个文件夹结构,该结构与您在intent中发送的路径相匹配。
    http://labs.makemachine.net/2010/03/simple-android-photo-capture/comment-page-1/#comment-143

    您需要在设备上手动创建图像/媒体文件夹。它们不会通过尝试在那里保存文件来自动创建。

        2
  •  0
  •   Mohammedsalim Shivani    8 年前

    这就是我要找的

    目录结构我正在通过我捕获的Uri数据,现在我可以显示图像。

    无需使用硬编码图像路径。

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