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

后端 未结 9 1944
梦谈多话
梦谈多话 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:18

    directly you can use this code it will open all type of files

    Intent sharingIntent = new Intent(Intent.ACTION_VIEW);
        Uri screenshotUri = Uri.fromFile(your_file);
        sharingIntent.setType("image/png");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
        String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(screenshotUri.toString()));
        sharingIntent.setDataAndType(screenshotUri, type == null ? "text/plain" : type);
        startActivity(Intent.createChooser(sharingIntent, "Share using"));
    

提交回复
热议问题