如何用 intent.ACTION_VIEW 显示图像
How to display image with intent.ACTION_VIEW
我的语法在 android 5.1 可以 运行 但在 android 7.1 就不行....
File file = new File(Environment.getExternalStorageDirectory(), "Pictures/1481853170451.jpg");
Toast.makeText(MainActivity.this, file.getPath(), Toast.LENGTH_LONG).show();
Uri path = Uri.fromFile(file);
if (file.exists()) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(path, "image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
this.startActivity(intent);
} catch (ActivityNotFoundException e) {
}
谁能告诉我可能的答案。提前谢谢你....
试试这个,
相机权限
private static final int REQUEST_STORAGE = 112;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_STORAGE );
} else {
getImage();
}
} else {
getImage();
}
}
});
}
获取权限结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getImage();
}
}
}
}
检查棉花糖的权限
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
从外部存储中获取图像
public void getImage()
{
File file = new File(Environment.getExternalStorageDirectory(), "Pictures/1481853170451.jpg");
Toast.makeText(MainActivity.this, file.getPath(), Toast.LENGTH_LONG).show();
Uri path = Uri.fromFile(file);
if (file.exists()) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(path, "image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
this.startActivity(intent);
} catch (ActivityNotFoundException e) {
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
只需完成以下link
步骤如下
1) 在 AndroidManifest.xml 中,在应用程序标签内添加以下提供程序
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
2) 在 res/xml 文件夹中创建 provider_paths.xml,示例内容如下
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="internal_files" path="DATA/TITLE/"/>
</paths>
3) 在Activity中创建图像文件,路径如下
context.getFilesDir() + File.separator + "DATA" + File.separator + "TITLE"+ File.separator + "sample_image_file"
4) 使用以下代码打开图像文件
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri photoURI = FileProvider.getUriForFile(context,
BuildConfig.APPLICATION_ID + ".provider",
new File(context.getFilesDir() + File.separator + "DATA" + File.separator + "TITLE"+ File.separator + "sample_image_file"));
intent.setDataAndType(photoURI,"image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(intent, "View using"));
我的语法在 android 5.1 可以 运行 但在 android 7.1 就不行....
File file = new File(Environment.getExternalStorageDirectory(), "Pictures/1481853170451.jpg");
Toast.makeText(MainActivity.this, file.getPath(), Toast.LENGTH_LONG).show();
Uri path = Uri.fromFile(file);
if (file.exists()) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(path, "image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
this.startActivity(intent);
} catch (ActivityNotFoundException e) {
}
谁能告诉我可能的答案。提前谢谢你....
试试这个,
相机权限
private static final int REQUEST_STORAGE = 112;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_STORAGE );
} else {
getImage();
}
} else {
getImage();
}
}
});
}
获取权限结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getImage();
}
}
}
}
检查棉花糖的权限
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
从外部存储中获取图像
public void getImage()
{
File file = new File(Environment.getExternalStorageDirectory(), "Pictures/1481853170451.jpg");
Toast.makeText(MainActivity.this, file.getPath(), Toast.LENGTH_LONG).show();
Uri path = Uri.fromFile(file);
if (file.exists()) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(path, "image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
this.startActivity(intent);
} catch (ActivityNotFoundException e) {
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
只需完成以下link
步骤如下
1) 在 AndroidManifest.xml 中,在应用程序标签内添加以下提供程序
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
2) 在 res/xml 文件夹中创建 provider_paths.xml,示例内容如下
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="internal_files" path="DATA/TITLE/"/>
</paths>
3) 在Activity中创建图像文件,路径如下
context.getFilesDir() + File.separator + "DATA" + File.separator + "TITLE"+ File.separator + "sample_image_file"
4) 使用以下代码打开图像文件
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri photoURI = FileProvider.getUriForFile(context,
BuildConfig.APPLICATION_ID + ".provider",
new File(context.getFilesDir() + File.separator + "DATA" + File.separator + "TITLE"+ File.separator + "sample_image_file"));
intent.setDataAndType(photoURI,"image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(intent, "View using"));