Take photo w/ camera intent and display in imageView or textView?

后端 未结 2 1238
一向
一向 2020-12-30 10:54

I have a question about how to take an image using the camera intent (or camera API) and then bring the image into an imageView for me to display in my application. This is

相关标签:
2条回答
  • 2020-12-30 11:30

    The method which i found to be easy and helpful is this:

    MainActivity

    private static String root = null;
    private static String imageFolderPath = null;        
    private String imageName = null;
    private static Uri fileUri = null;
    private static final int CAMERA_IMAGE_REQUEST=1;
    
    public void captureImage(View view) {
    
        ImageView imageView = (ImageView) findViewById(R.id.capturedImageview);
    
                 // fetching the root directory
         root = Environment.getExternalStorageDirectory().toString()
         + "/Your_Folder";
    
         // Creating folders for Image
         imageFolderPath = root + "/saved_images";
         File imagesFolder = new File(imageFolderPath);
         imagesFolder.mkdirs();
    
        // Generating file name
        imageName = "test.png";
    
        // Creating image here
    
        File image = new File(imageFolderPath, imageName);
    
        fileUri = Uri.fromFile(image);
    
        imageView.setTag(imageFolderPath + File.separator + imageName);
    
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    
        startActivityForResult(takePictureIntent,
                CAMERA_IMAGE_REQUEST);
    
    }
    

    and then in your activity onActivityResult method:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
    
        if (resultCode == RESULT_OK) {
    
            switch (requestCode) {
            case CAMERA_IMAGE_REQUEST:
    
                Bitmap bitmap = null;
                try {
                    GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
                    bitmap = getImageThumbnail.getThumbnail(fileUri, this);
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
    
                // Setting image image icon on the imageview
    
                ImageView imageView = (ImageView) this
                        .findViewById(R.id.capturedImageview);
    
                imageView.setImageBitmap(bitmap);
    
                break;
    
            default:
                Toast.makeText(this, "Something went wrong...",
                        Toast.LENGTH_SHORT).show();
                break;
            }
    
        }
    }
    

    GetImageThumbnail.java

    public class GetImageThumbnail {
    
    private static int getPowerOfTwoForSampleRatio(double ratio) {
        int k = Integer.highestOneBit((int) Math.floor(ratio));
        if (k == 0)
            return 1;
        else
            return k;
    }
    
    public Bitmap getThumbnail(Uri uri, Context context)
            throws FileNotFoundException, IOException {
        InputStream input = context.getContentResolver().openInputStream(uri);
    
        BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
        onlyBoundsOptions.inJustDecodeBounds = true;
        onlyBoundsOptions.inDither = true;// optional
        onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
        BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
        input.close();
        if ((onlyBoundsOptions.outWidth == -1)
                || (onlyBoundsOptions.outHeight == -1))
            return null;
    
        int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
                : onlyBoundsOptions.outWidth;
    
        double ratio = (originalSize > 400) ? (originalSize / 350) : 1.0;
    
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
        bitmapOptions.inDither = true;// optional
        bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
        input = context.getContentResolver().openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
        input.close();
        return bitmap;
    }
    }
    

    and then on the ImageView onclick method will be like this:

    public void showFullImage(View view) {
        String path = (String) view.getTag();
    
        if (path != null) {
    
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri imgUri = Uri.parse("file://" + path);
            intent.setDataAndType(imgUri, "image/*");
            startActivity(intent);
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-30 11:49

    To take photo correctly you should store it in temp file, because data in result intent can be null:

    final Intent pickIntent = new Intent();
    pickIntent.setType("image/*");
    pickIntent.setAction(Intent.ACTION_GET_CONTENT);
    final String pickTitle = activity.getString(R.string.choose_image);
    final Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
    if (AvailabilityUtils.isExternalStorageReady()) {
        final Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        final Uri fileUri = getCameraTempFileUri(activity, true);
        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
        new Intent[] { takePhotoIntent });
    }
    
    activity.startActivityForResult(chooserIntent, REQUEST_CODE);
    

    And then get photo from Uri:

    if (requestCode == ProfileDataView.REQUEST_CODE
                    && resultCode == Activity.RESULT_OK) {
                final Uri dataUri = data == null ? getCameraTempFileUri(context,
                    false) : data.getData();
                    final ParcelFileDescriptor pfd = context.getContentResolver()
                        .openFileDescriptor(imageUri, "r");
                    final FileDescriptor fd = pfd.getFileDescriptor();
                    final Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fd);
            }
    
    0 讨论(0)
提交回复
热议问题