Get Image path from camera intent

前端 未结 7 1643
轮回少年
轮回少年 2021-01-05 13:15

There is option of capture image from camera in my application.But there is problem to get image from camera. When i use ACTION_IMAGE_CAPTURE this it return nul

相关标签:
7条回答
  • 2021-01-05 13:45

    try this one

     Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i , 0);          
    

    and call this

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent resultData) {
        super.onActivityResult(requestCode, resultCode, resultData);
    
            if (resultData != null) {
    
            String[] projection = { MediaStore.Images.Media.DATA };
                    Cursor cursor = managedQuery(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            projection, null, null, null);
                    int column_index_data = cursor
                            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    cursor.moveToLast();
    
                    String imagePath = cursor.getString(column_index_data);
                    Bitmap bitmapImage = BitmapFactory.decodeFile(imagePath );
                    imageView.setImageBitmap(bitmapImage );
    
                }
    

    and use the prrmission what you want

     <uses-permission android:name="android.permission.CAMERA" /> 
        <permission android:name="android.permission.FLASHLIGHT" />
        <uses-feature android:name="android.hardware.camera"/>
    
    0 讨论(0)
  • 2021-01-05 13:47
     Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
             if(cursor != null && cursor.moveToFirst())
             {
                 do {
                     Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
                     String photoPath = uri.toString();
                 }while(cursor.moveToNext());
    
    
                 cursor.close();
             }
    

    when while loop doing the last itration it give the image path which in captured last time.

    0 讨论(0)
  • 2021-01-05 13:48

    You're probably running a Samsung device. I believe this is a bug that occurs in these devices. You have to get the URI otherwise. Do this:

    /**
      * (This is a variable instance) Contains the path and file name where you want to save the image.
      * Mainly used to start Intent.Action_View with this URI. (GalleryApp)
      */
    private Uri uriImage= null;
    
    public void onClickCamera(View v){
        // Cria uma intent para capturar uma imagem e retorna o controle para quem o chamou (NAO PRECISA DECLARAR PERMISSAO NO MANIFESTO PARA ACESSAR A CAMERA POIS O FAZEMOS VIA INTENT).
        // Creates an intent to capture an image and returns control to the caller
        Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE );
        // Cria um arquivo para salvar a imagem.
        // Creates an file to save the image.
        uriImage = ProcessaImagens.getOutputMediaFileUri( ProcessaImagens.MEDIA_TYPE_IMAGE, getActivity().getApplicationContext() );
        // Intent to pass a URI object containing the path and file name where you want to save the image. We'll get through the data parameter of the method onActivityResult().
        intent.putExtra( MediaStore.EXTRA_OUTPUT, uriImagem );
        // Starts the intent to image capture and waits the result.
        startActivityForResult( intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
    }
    

    The ProcessaImagens class is a class that I coded and I'm sharing with everyone. Can use this class to facilitate. It has a very good method to compress the image if you want to save images in the database. In its onActivityResult() method do this:

    @Override
    public void onActivityResult( int requestCode, int resultCode, Intent data ) {
        // If finished activity on startForActivityResult.
        if ( resultCode == Activity.RESULT_OK ) {
            if ( requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE ) {
                String imagePath = uriImage.getPath();
                // I will compress the imagem. Read the javadoc from method and you'll see which it returns both bitmap and array bytes.
                List<Object> imageCompact = ProcessaImagens.compactarImagem( uriImagem.getPath() );
                Bitmap imageBitmap = (Bitmap) imageCompact.get( 0 );
                byte[] imageBytes = (byte[]) imageCompact.get( 1 );
    
            }
        }
        // If canceled activity on startForActivityResult.
        else if ( resultCode == Activity.RESULT_CANCELED ) {
            if ( requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE ) {
                // User canceled the image capture.
                Log.d( getTag(), "Image capture canceled!" );
            }
        }
        // If an error occurred in the activity on startForActivityResult.
        else {
            // Failed the image capture, alert the user.
            Toast.makeText( getActivity().getApplicationContext(), "FAILED! The image capture failed!", Toast.LENGTH_LONG ).show();
            Log.e( getTag(), "FAILED! The image capture failed!" );
        }
    }
    

    Note that I used getActivity().getApplicationContext() because I'm getting the context from a Fragment and not from Activity. I believe with this method you can have what you want. Just make little changes how form to get context.

    0 讨论(0)
  • 2021-01-05 13:53

    Make sure you have put this tag in your manifest file: <uses-feature android:name="android.hardware.camera" />

    And this might be help you: https://stackoverflow.com/a/10779050/2514032

    0 讨论(0)
  • 2021-01-05 13:54

    you can use following steps:

    // create a global variable
    File destination;
    openCameraBtn.setOnClickListener(new OnClickListener() 
    {
    
                @Override
                public void onClick(View v) {
                     destination = new   File(Environment.getExternalStorageDirectory(),"image.jpg");
    
                     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
                     startActivityForResult(intent, CAMERA_PICTURE);
    
                }
            });
    
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
    
    
        if(requestCode == CAMERA_PICTURE && resultCode == Activity.RESULT_OK) 
               {
               try{
                FileInputStream in = new FileInputStream(destination);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 10; //Downsample 10x
                Log.d("PP", " bitmap factory=========="+options);
                Bitmap user_picture_bmp = BitmapFactory.decodeStream(in, null, options);
            userImage.setImageBitmap(user_picture_bmp);
            } catch (Exception e) 
              { e.printStackTrace();
             }
    
        } 
    

    please declare permission and feature in your manifest file.

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    

    if u want in more detail then you can use following link:

    http://developer.android.com/guide/topics/media/camera.html

    i hope you will success.

    0 讨论(0)
  • 2021-01-05 14:02

    If you successfully implement the code you will get the image that is captured then manually you can save it for future use.

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
            if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                videoView.setVisibility(View.INVISIBLE);
                imageView.setVisibility(View.VISIBLE);
                imageView.setImageBitmap(photo);
            }
        } 
    
    0 讨论(0)
提交回复
热议问题