裁切图像位于内部存储 [英] Crop Image Located in Internal Storage
问题描述
我正在使用Android的作物意图有麻烦。
我将图像存储在内置存储分配我的应用程序,我希望能够作物他们,然后将它们设置为墙纸。
我可以用一个形象的工作存储在外部存储和使用该code:
cropIntent.setDataAndType(Uri.fromFile(新文件(URI)),图像/ *);
在我下面的意图无法启动作物的意图和0的结果code返回执行code。
意图cropIntent =新的意向书(com.android.camera.action.CROP);
cropIntent.setDataAndType(FileProvider.getUriForFile(活动,com.example.test.fileprovider,新文件(URI)),图像/ *);
cropIntent.putExtra(裁剪,真);
cropIntent.putExtra(aspectX,9);
cropIntent.putExtra(aspectY,16);
cropIntent.putExtra(回归数据,真正的);
cropIntent.putExtra(规模化,真正的);
activity.startActivityForResult(cropIntent,PIC_CROP);
在默认情况下,保存在内部存储的文件是私有的,你的应用程序和其他应用程序不能访问这些文件(也不能在用户)。
ACTION_CROP
启动一个其它应用,支持种植,并通过它的URI文件裁剪。如果该文件是在内部存储,它是不能直接访问裁剪应用
使用 FileProvider
以提供内部文件到其他应用程序需要进行一些配置。从设置文件共享| Android开发:
指定FileProvider
定义FileProvider为您的应用程序需要在您的清单中的条目。此项指定生成内容的URI,以及一个XML文件,指定的目录你的应用程序可以共享的名称使用授权。
<剪断>
指定可共享目录
一旦添加了FileProvider到您的应用程序清单,你需要指定包含要共享的文件的目录。要指定目录,通过创建项目的RES / XML /子目录中的文件filepaths.xml启动。
下面的 FileProvider
样品集成的code和成功打开作物活动空间(,图像上的Nexus 5测试普通的Android 5.1.1上):
AndroidManifest.xml中
<应用
...>
<供应商
机器人:名称=android.support.v4.content.FileProvider
机器人:当局=com.example.test.fileprovider
机器人:grantUriPermissions =真
机器人:出口=假>
&所述;元数据
机器人:名称=android.support.FILE_PROVIDER_PATHS
机器人:资源=@ XML /文件路径/>
< /供应商>
...
< /用途>
RES / XML / filepaths.xml
<路径>
<文件路径路径=图像/NAME =图片/>
< /路径>
MainActivity.java
//手动保存测试图像
最终文件的ImagePath =新的文件(getFilesDir(),图像);
最终文件镜像文件=新的文件(的ImagePath,sample.jpg);
//提供权威字符串必须在AndroidManifest.xml中声明的一场比赛
最后乌里providedUri = FileProvider.getUriForFile(
MainActivity.this,com.example.test.fileprovider,镜像文件);
意图cropIntent =新的意向书(com.android.camera.action.CROP);
cropIntent.setDataAndType(providedUri,图像/ *);
cropIntent.putExtra(裁剪,真);
cropIntent.putExtra(aspectX,9);
cropIntent.putExtra(aspectY,16);
cropIntent.putExtra(回归数据,真正的);
cropIntent.putExtra(规模化,真正的);
//异常,如果不授予读取权限将被抛出
cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(cropIntent,PIC_CROP);
I am having trouble using the crop intent in Android.
I am storing images in the internal storage allocation for my app and I want to be able to crop them and then set them as the wallpaper.
I can get it to work with an image stored in external storage and using this code:
cropIntent.setDataAndType(Uri.fromFile(new File(uri)), "image/*");
When I execute the code below the intent fails to launch the crop intent and a 0 result code is returned.
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(FileProvider.getUriForFile(activity, "com.example.test.fileprovider", new File(uri)), "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 9);
cropIntent.putExtra("aspectY", 16);
cropIntent.putExtra("return-data", true);
cropIntent.putExtra("scale", true);
activity.startActivityForResult(cropIntent, PIC_CROP);
From Storage Options | Android Developers:
By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user).
ACTION_CROP
launches an "other application" that supports cropping, and passes it the URI of the file to crop. If that file is in internal storage, it is not directly accessible to the cropping application.
Using a FileProvider
to provide an internal file to other apps requires some configuration. From Setting Up File Sharing | Android Developers:
Specify the FileProvider
Defining a FileProvider for your app requires an entry in your manifest. This entry specifies the authority to use in generating content URIs, as well as the name of an XML file that specifies the directories your app can share.
<snip>
Specify Sharable Directories
Once you have added the FileProvider to your app manifest, you need to specify the directories that contain the files you want to share. To specify the directories, start by creating the file filepaths.xml in the res/xml/ subdirectory of your project.
The following FileProvider
sample integrates your code and successfully opens the crop activity on an image in internal storage (tested on a Nexus 5, stock Android 5.1.1):
AndroidManifest.xml
<application
...>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.test.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
...
</application>
res/xml/filepaths.xml
<paths>
<files-path path="images/" name="images" />
</paths>
MainActivity.java
// Manually stored image for testing
final File imagePath = new File(getFilesDir(), "images");
final File imageFile = new File(imagePath, "sample.jpg");
// Provider authority string must match the one declared in AndroidManifest.xml
final Uri providedUri = FileProvider.getUriForFile(
MainActivity.this, "com.example.test.fileprovider", imageFile);
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(providedUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 9);
cropIntent.putExtra("aspectY", 16);
cropIntent.putExtra("return-data", true);
cropIntent.putExtra("scale", true);
// Exception will be thrown if read permission isn't granted
cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(cropIntent, PIC_CROP);
这篇关于裁切图像位于内部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!