Open a selected file (image, pdf, …) programmatically from my Android Application?

后端 未结 9 1942
梦谈多话
梦谈多话 2020-12-02 17:34

I\'m working on an Android application which should be able to open a selected file from a specific folder.

I already tried this, but after selecting which applicati

9条回答
  •  生来不讨喜
    2020-12-02 18:05

    Try the following code.

    File file = new File(path); // path = your file path
    lastSlash = file.toString().lastIndexOf('/');
    
    if (lastSlash >= 0)
    {
        fileName = url.toString().substring(lastSlash + 1);
    }
    
    
    if (fileName.endsWith("pdf"))
    {
        mimeType = "application/pdf";
    }
    else
    {
        mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension
        (MimeTypeMap.getFileExtensionFromUrl(path));
    }
    
    Uri uri_path = Uri.fromFile(file);
    
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    intent.putExtra(PATH, path);
    intent.putExtra(MIMETYPE, mimeType);
    intent.setType(mimeType);
    intent.setDataAndType(uri_path, mimeType);
    
    startActivity(intent);
    

提交回复
热议问题