no news
no news

Reputation: 1070

Open file by URL

I have FileOpener class for open documents by URL:

public class FileOpener {


    private FileOpener() {
    }

    private static String url;
    private static Uri uri;
    private static Intent intent;

    public static void open(Context context, FileDTO fileDTO) {
        fileDTO.setUrl("https://calibre-ebook.com/downloads/demos/demo.docx");
        setupBase(fileDTO);
        checkFileTypeByUrl();

        try {
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void checkFileTypeByUrl() {

    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
    intent.setDataAndType(uri, mimeType);
    }

    private static void setupBase(FileDTO fileDTO) {
        url = fileDTO.getUrl();
        uri = Uri.parse(url);
        intent = new Intent(Intent.ACTION_VIEW, uri);
    }
}

In activity/fragment it's look like this:

    @Override
    public void openOrDownloadFile(FileDTO file) {
        FileOpener.open(getContext(), file);
    }

It's work fine for PDF/Txt documents, but for MsWord/MsExcel/MsPowerPoint have ActivityNotFoundException (I have word/excel/powerpoint on my device, which opening from other applications without problem)

Stacktrace:

W/System.err: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://calibre-ebook.com/downloads/demos/demo.docx typ=application/vnd.openxmlformats-officedocument.wordprocessingml.document flg=0x4000000 }

What could be the problem?

Upvotes: 1

Views: 4624

Answers (2)

Vladyslav Matviienko
Vladyslav Matviienko

Reputation: 10871

If you see an example of ACTION_VIEW intent filter for the app to open files:

<intent-filter android:label="app">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="https"
          android:host="www.example.com">
</intent-filter>

You can see that there is a android:scheme="https", which means that this app will accept ACTION_VIEW only with Uris having https scheme.
In your case the word app has only file scheme, and won't handle http or https Uris.

So first you have to download the file, and only after that open it as local file.

Upvotes: 1

AskNilesh
AskNilesh

Reputation: 69689

try this using web view you can load your file from url

WebView webview = (WebView)findViewById(R.id.containWebView);
webview.setWebViewClient(new AppWebViewClients());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url="
            + "YOUR_DOC_URL_HERE"); 

 public class AppWebViewClients extends WebViewClient {



@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub
    view.loadUrl(url);
    return true;
}

@Override
public void onPageFinished(WebView view, String url) {
    // TODO Auto-generated method stub
    super.onPageFinished(view, url);

}
}

Upvotes: 1

Related Questions