Android:使用uri拍照

时间:2018-11-13 19:11:53

标签: android android-camera-intent android-fileprovider

我正在尝试使用Uri在Android中拍照。但是我正在努力从创建的Uri获取位图。我总是从Uri中获取空对象。

Uri imgUri;
File newfile;
public static Button camButton;
public static ImageView img;
public static TextView text;

public void photo(Button cB, ImageView im, TextView tv) {

    text = tv;
    camButton = cB;
    img = im;

    camButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
            File newdir = new File(dir);
            newdir.mkdirs();
            String file = dir + "test" + ".png";
            newfile = new File(file);
            try {
                newfile.createNewFile();
            } catch (IOException e) {
            }

            imgUri = FileProvider.getUriForFile(MainActivity.this,
                    BuildConfig.APPLICATION_ID + ".provider",
                    newfile);

            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
            startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {

        Uri imageUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", newfile);

        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
        } catch (Exception e) {
            e.printStackTrace();
        }

        bitmap = Bitmap.createBitmap(bitmap,0,((bitmap.getHeight()-bitmap.getWidth()))/2,bitmap.getWidth(),bitmap.getWidth());
        bitmap = Bitmap.createScaledBitmap(bitmap, 64, 64, true);
        img.setImageBitmap(bitmap);

    }
}

我遇到错误Could not write image : java.io.FileNotFoundException: open failed: ENOENT (No such file or directory) 因此,无法加载位图,并且我的位图为null,因此我收到了NullpointerException

1 个答案:

答案 0 :(得分:-1)

try with this: 
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

            Uri selectedImage = data.getData();
            Bitmap photo = null;
            try {
                photo = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
            } catch (IOException e) {
                e.printStackTrace();
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);

            byte[] b = baos.toByteArray();

            String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
 byte[] decodedString = Base64.decode(encoded, Base64.DEFAULT);
                Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                CircularImageView imageView = findViewById(R.id.profileimage);
                imageView.setImageBitmap(decodedByte);


        }
    }