【问题标题】:Android: Take picture with uriAndroid:用uri拍照
【发布时间】:2018-11-13 19:11:53
【问题描述】:

我正在尝试使用 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) 因此,无法加载位图并且我的位图为空,因此我收到 NullpointerException

【问题讨论】:

  • newfile.createNewFile() 成功了吗?

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


【解决方案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);


        }
    }

【讨论】:

    猜你喜欢
    • 2015-11-27
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    相关资源
    最近更新 更多