【问题标题】:android: how to open a file out of my appandroid:如何从我的应用程序中打开文件
【发布时间】:2014-06-20 08:43:18
【问题描述】:

我正在开发一个应用程序,它应该在解码后打开一个文件。 所以,我想打开一个默认应用程序在我的应用程序中选择的文件(例如图像)(如果这种文件有多个应用程序,那么用户可以选择其中一个应用程序)。

【问题讨论】:

    标签: android eclipse file


    【解决方案1】:

    对于不同的文件类型,您需要使用不同的意图,如下所示

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,
    "Open image"), 1);
    

    或者您可以使用以下代码从文件中获取文件类型,并让android系统确定可以使用哪个应用程序打开它

    File file = new File("EXAMPLE.JPG");
    
    
    String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(".JPG");
    //You need to find the extension from the file using a split command
    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), mime);
    startActivityForResult(intent, 10);
    

    【讨论】:

    • 非常感谢!我没有拆分,而是使用了这个: String ext = MimeTypeMap.getFileExtensionFromUrl(file2open);在此之前: String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);其余的工作正常。
    • 对不起,我忘了。再次非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2011-09-18
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 2015-04-01
    • 2014-07-11
    • 1970-01-01
    相关资源
    最近更新 更多