Bad*_*ari 76 android android-image
我已经提取了图像uri,现在我想用Android的默认图像查看器打开图像.甚至更好,用户可以选择用于打开图像的程序.如果您尝试打开文件,File Explorers之类的东西会提供给您.
Vik*_*kas 157
接受的答案对我不起作用,
工作原理:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
and*_*per 28
如果您的应用针对Android N(7.0)及更高版本,则不应使用上述答案("Uri.fromFile"方法),因为它不适合您.
相反,您应该使用ContentProvider.
例如,如果您的图像文件位于外部文件夹中,则可以使用此文件(类似于我在此处创建的代码):
File file = ...;
final Intent intent = new Intent(Intent.ACTION_VIEW)//
.setDataAndType(VERSION.SDK_INT >= VERSION_CODES.N ?
FileProvider.getUriForFile(this,getPackageName() + ".provider", file) : Uri.fromFile(file),
"image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Run Code Online (Sandbox Code Playgroud)
表现:
<provider
android:name="androidx.core.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>
Run Code Online (Sandbox Code Playgroud)
res/xml/provider_paths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!--<external-path name="external_files" path="."/>-->
<external-path
name="files_root"
path="Android/data/${applicationId}"/>
<external-path
name="external_storage_root"
path="."/>
</paths>
Run Code Online (Sandbox Code Playgroud)
如果您的图像位于应用程序的私有路径中,您应该创建自己的ContentProvider,因为我在链接上创建了"OpenFileProvider".
Bad*_*ari 27
问自己,也回答自己:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
Run Code Online (Sandbox Code Playgroud)
它还会询问用于查看文件的程序.
Eug*_*ene 20
试试吧:
Uri uri = Uri.fromFile(entry);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
String mime = "*/*";
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
if (mimeTypeMap.hasExtension(
mimeTypeMap.getFileExtensionFromUrl(uri.toString())))
mime = mimeTypeMap.getMimeTypeFromExtension(
mimeTypeMap.getFileExtensionFromUrl(uri.toString()));
intent.setDataAndType(uri,mime);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
Joa*_*huk 17
基于Vikas的答案,但稍作修改:Uri由参数接收:
private void showPhoto(Uri photoUri){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(photoUri, "image/*");
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
小智 8
如果您使用Android N及以下版本,这可能会有所帮助
File file=new File(Environment.getExternalStorageDirectory()+"/directoryname/"+filename);
Uri path= FileProvider.getUriForFile(MainActivity.this,BuildConfig.APPLICATION_ID + ".provider",file);
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path,"image/*");
intent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); //must for reading data from directory
Run Code Online (Sandbox Code Playgroud)
一个更干净,更安全的解决方案(您实际上不应该对字符串进行硬编码):
public void openInGallery(String imageId) {
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.buildUpon().appendPath(imageId).build();
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
您所要做的就是将图像ID附加到EXTERNAL_CONTENT_URI的路径末尾。然后使用“视图”操作和Uri启动一个Intent。
图像ID来自查询内容解析器。
| 归档时间: |
|
| 查看次数: |
144867 次 |
| 最近记录: |