我正在寻找一种方法,使用Android支持库的FileProvider正确地与外部应用程序共享(而不是OPEN)内部文件.
按照文档上的示例,
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.supportv4.my_files"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/my_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
并使用ShareCompat将文件共享给其他应用程序,如下所示:
ShareCompat.IntentBuilder.from(activity)
.setStream(uri) // uri from FileProvider
.setType("text/html")
.getIntent()
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
Run Code Online (Sandbox Code Playgroud)
不起作用,因为FLAG_GRANT_READ_URI_PERMISSION仅授予data对intent 指定的Uri的权限,而不授予EXTRA_STREAMextra 的值(由set设置setStream).
我试图通过设置成安全android:exported到true的供应商,但FileProvider在内部检查,如果本身是出口,所以时,它抛出一个异常.
android android-contentprovider android-support-library android-fileprovider
我正在尝试将Camera App存储到我的内部存储器中.我也理解第三方应用程序无法访问我的应用程序的内部存储,但我们可以通过公开内部目录来实现FileProvider.我在这里遵循了指南:
我AndroidManifest.xml用以下方式指定我:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stackoverflow.test.camerawithfileprovider" >
<application>
<activity
...
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.stackoverflow.test.camerawithfileprovider.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_path" />
</provider>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
我创建了一个名为xml文件file_provider_path.xml中/res/xml/:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="image_capture" path="public/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
这就是我创建和调用Camera Intent的方法:
private static final int CAMERA_REQUEST_CODE = 5000;
private static final String CAMERA_FP_AUTHORITY = "com.stackoverflow.test.camerawithfileprovider.fileprovider";
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Context context = MainActivity.this;
File imagePath = new File(context.getFilesDir(), …Run Code Online (Sandbox Code Playgroud) 我想在用户点击按钮时打开PDF文件.目前,我正在使用此代码来实现此目的:
Uri path = Uri.fromFile(new File("file:///android_asset/regola11_1.pdf"));
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(pdfIntent);
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
当我选择使用Adobe Acrobat时,我会收到一条显示为Toast的消息
"This file could not be accessed Check the location or the network and try again."
Run Code Online (Sandbox Code Playgroud)
当我尝试使用Drive PDF Viewer时,我明白了
"Cannot display PDF ( regola11_1.pdf cannot be opened)"
Run Code Online (Sandbox Code Playgroud)
PDF文件存储在
app > build > intermediates > assets
Run Code Online (Sandbox Code Playgroud)
问题出在哪儿?
编辑
现在我使用以下代码:
File file = new File("\"file:///android_asset/regola11_1.pdf");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
context.startActivity(intent);
}
catch (ActivityNotFoundException e) { …Run Code Online (Sandbox Code Playgroud) 我有一个Android 7.0测试设备和我的APK目标="targetSdkVersion 22",有:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
有:
final File f = new
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "DressUpBaby" + photonumber + ".png");
f.createNewFile();
Run Code Online (Sandbox Code Playgroud)
在这一点上,我得到警告:
W/System.err: java.io.IOException: Permission denied
Run Code Online (Sandbox Code Playgroud)
如何保存文件?当我在Eclipse上创建它时,这是有效的,但是现在我已经更新并转移到Android Studio,它似乎已经破坏了一些东西.
我试图允许用户访问他的照片库以获取个人资料图片,并将该个人资料图片保存到 SharedPreferences。我还有一个从 SharedPreferences 获取这张图片的导航栏
我收到以下错误:
java.lang.SecurityException:
Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord
{a601c1c 3379:com.example.foo.finalapp/u0a60} (pid=3379, uid=10060) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
Run Code Online (Sandbox Code Playgroud)
这是获取图片的代码:
主要活动
ImageView prof_pic = (ImageView) header.findViewById(R.id.profPic);
pref = getSharedPreferences(Profile.pref_filename, 0);
String uri = pref.getString("target_uri", "");
TextView tv_name = (TextView) header.findViewById(R.id.tv_name);
String name = pref.getString("name", "");
if(!uri.equals("")) {
Uri urii = Uri.parse(uri);
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(urii));
tv_name.setText(name);
prof_pic.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
}
}
else {
prof_pic.setImageResource(R.drawable.ic_android_black_24dp);
}
}
profile_pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View …Run Code Online (Sandbox Code Playgroud) 我试图在拍照或拍照后从Uri裁剪图像.我的代码是这样的:
public static void cropImage(Uri uri, Activity activity, int action_code) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 600);
intent.putExtra("outputY", 600);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (intent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivityForResult(intent, action_code);
} else {
Toast.makeText(activity, "No Crop App Available", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
onActivityResult()像这样覆盖:
if (resultCode == Activity.RESULT_OK && requestCode == Utils.CODE_CROP_IMAGE) {
Bundle extras = data.getExtras();
showCenterToast("ccc");
if (extras != null) {
showCenterToast("CCC");
Bitmap photo = extras.getParcelable("data");
ivAvatar.setImageBitmap(photo); // display image …Run Code Online (Sandbox Code Playgroud) 我在尝试在 Android 模拟器中执行应用程序升级时遇到了一些问题。该场景的流程来自一个 Activity,我将执行AsyncTask A打开片段 A,然后在AsyncTask A内,我将检查版本升级是否可用。
如果可用并且用户从片段 A中选择了“确定” ,我将继续执行AsyncTask B打开片段 B,该片段向用户显示一条消息,表示升级正在进行中。在AsyncTask B doInBackground()中,我将执行install(),在onPostExecute()中,我将显示成功消息。
在我执行安装的 AsyncTask B 中:
@Override
protected Boolean doInBackground(Void... params) {
boolean ret= viewmodel.installApk(mActivity);
return ret;
}
Run Code Online (Sandbox Code Playgroud)
在我的视图模型类中:
public boolean installApk(Activity mActivity){
boolean success = false;
String fullPath = scanDirectoryForApk();
System.out.println("INSTALLING APK ......................... ");
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri apkURI = FileProvider.getUriForFile(mActivity, mActivity.getApplicationContext().getPackageName() + ".provider", new File(fullPath));
intent.setDataAndType(apkURI, …Run Code Online (Sandbox Code Playgroud)