在Android的默认图库图像查看器中使用URI打开图像

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)

  • 这个答案最终以`file:/// sdcard ...` - 这是故意的吗? (5认同)
  • 是的,`文件f =新文件(......,...); if(f.isFile()){MediaScannerConnection.scanFile(SchedaActivity.this,new String [] {f.toString()},null,new MediaScannerConnection.OnScanCompletedListener(){@ Overver public void onScanCompleted(String path,Uri uri) {Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri,"image/*"); SchedaActivity.this.startActivityForResult(intent,SchedaActivity.this.ACTIVITY_CODE);}});` (4认同)
  • 是的,但你如何在Android 2.2和2.3上避免10秒黑屏等待,而画廊说它搜索新的fotos和专辑? (2认同)

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)

它还会询问用于查看文件的程序.

  • 嗨哈里,你的代码对打开图像非常有用.你有办法打开画廊的文件夹吗?这是我的问题http://stackoverflow.com/questions/6519024/access-specific-folder-of-gallery-in-android (2认同)

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)


Chr*_*rry 5

一个更干净,更安全的解决方案(您实际上不应该对字符串进行硬编码):

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 次

最近记录:

6 年,4 月 前