获取res/drawable目录下的图片Uri

获取res/drawable目录下的图片Uri:

/**
     * 得到资源文件中图片的Uri
     * @param context 上下文对象
     * @param id 资源id
     * @return Uri
     */
    public Uri getUriFromDrawableRes(Context context, int id) {
        Resources resources = context.getResources();
        String path = ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + resources.getResourcePackageName(id) + "/"
                + resources.getResourceTypeName(id) + "/"
                + resources.getResourceEntryName(id);
        return Uri.parse(path);
    }
在拍照时获取图片uri,可以通过以下步骤实现: 1. 在AndroidManifest.xml文件中添加文件提供器(FileProvider): ```xml <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ``` 2. 在res/xml文件夹中创建一个名为file_paths.xml的文件,并在其中指定保存图片的路径: ```xml <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-files-path name="my_images" path="Pictures" /> </paths> ``` 3. 在拍照的Activity中,创建一个文件对象并将其保存在指定的路径下: ```java File photoFile = null; try { photoFile = File.createTempFile( "IMG_", /* prefix */ ".jpg", /* suffix */ getExternalFilesDir(Environment.DIRECTORY_PICTURES) /* directory */ ); } catch (IOException ex) { // Error occurred while creating the File ... } // Save a file: path for use with ACTION_VIEW intents String currentPhotoPath = photoFile.getAbsolutePath(); ``` 4. 启动相机应用,并在onActivityResult()方法中获取拍摄后的照片uri: ```java private static final int REQUEST_IMAGE_CAPTURE = 1; private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // Create the File where the photo should go File photoFile = null; try { photoFile = createImageFile(); } catch (IOException ex) { // Error occurred while creating the File ... } // Continue only if the File was successfully created if (photoFile != null) { Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileProvider", photoFile); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { // Get the uri of the captured image Uri uri = Uri.fromFile(photoFile); ... } } ``` 注意:在Android 10及以上版本中,需要使用FileProvider来获取文件uri,而不是直接使用file://协议。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦游人布拿拿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值