栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

拍照和保存图片

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

拍照和保存图片

private void openCamera(){
    File outputImage = new File(getExternalCacheDir(),getnormalmac(dev_mac)+".jpg");
    try {
        if(outputImage.exists()){
            outputImage.delete();
        }
        outputImage.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (Build.VERSION.SDK_INT>=24) {
        imageUri = FileProvider.getUriForFile(this,
                "com.example.newuieasyc.fileprovider",
                outputImage);
    }else{
        imageUri =Uri.fromFile(outputImage);
    }
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    mCameraForResult.launch(takePictureIntent);
}
  private void openGallery(){
        Intent mediaScanIntent = new Intent(Intent.ACTION_OPEN_document);
        mediaScanIntent.addCategory(Intent.CATEGORY_OPENABLE);
        mediaScanIntent.setType("image/*");
        mGalleryForResult.launch(mediaScanIntent);
    }
public String saveMyBitmap(Bitmap mBitmap, String bitName) {
    File f = new File(Environment.getExternalStorageDirectory().getPath()+"/Android/data/com.example.newuieasyc/" + bitName + ".jpg");
    try {
        FileOutputStream fOut = new FileOutputStream(f);
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 10, fOut);
        try {
            fOut.flush();
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return f.toString();
}
private final ActivityResultLauncher mCameraForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
        result -> {
            if (result.getResultCode() == Activity.RESULT_OK) {
                Bitmap bitmap = null;
                try {
                    bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                binding.icon.setImageBitmap(bitmap);
                imagePath=saveMyBitmap(bitmap,dev_mac);
                saveString(dev_mac+"pic", imagePath);
            } else {

            }
        });
private final ActivityResultLauncher mGalleryForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    imageUri=result.getData().getData();
                    if(imageUri!=null) {
                        Bitmap bit = null;
                        try {
                            bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        binding.icon.setImageBitmap(bit);
                        imagePath=saveMyBitmap(bit,dev_mac);
                        saveString(dev_mac+"pic", imagePath);
                    }
                }
            });

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/592118.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号