2011-08-10 16 views
7

मैं स्थानीय फ़ाइल खोलने के लिए ब्राउज़र को मंशा भेजने की कोशिश कर रहा हूं। मैं इस फ़ाइल को खोलने के लिए डिफ़ॉल्ट ब्राउज़र का उपयोग करना चाहता हूं।स्थानीय फ़ाइल खोलने के लिए ब्राउज़र लॉन्च कैसे करें

if(file.exists()){ 
    Log.d(TAG, "file.exists"); 
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file)); 
    context.startActivity(intent); 
} 

लेकिन यह मेरे और अपवाद

08-10 13:27:58.993: ERROR/AndroidRuntime(28453): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///sdcard/release_notes.htm } 

फेंकता मैं आशय ब्राउज़र निम्नलिखित का उपयोग करते हैं google.com खोलता उम्मीद

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com")); 

इसके अलावा जब मैं ब्राउज़र पता करने के लिए फ़ाइल यूआरएल (file:///sdcard/release_notes.htm) लिखने के रूप में बार यह इसे उम्मीद के रूप में खोलता है।

उत्तर

4

आपको इरादे में ब्राउज़ करने योग्य श्रेणी जोड़ने की आवश्यकता है।

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file)); 
intent.addCategory(Intent.CATEGORY_BROWSABLE); 
startActivity(intent); 
+5

'08-10 14: 03: 48.414: त्रुटि/एंड्रॉइड रनटाइम (2 9 612): android.content.ActivityNotFoundException: इरादा को संभालने के लिए कोई गतिविधि नहीं मिली {act = android.intent.action.VIEW बिल्ली = [android.intent.category.BROWSABLE] डेटा = फ़ाइल: ///sdcard/release_notes.htm} ' – roose

+0

हम्म, अजीब, मुझे अपने फोन पर इसके साथ कोई समस्या नहीं है :(- दोबारा जांचें कि जब आप इसे आज़माते हैं तो sdcard कंप्यूटर पर नहीं लगाया जाता है। – Zharf

+2

हो सकता है कि आप स्पष्ट रूप से इरादे से ब्राउजर क्लासनाम जोड़ने का प्रयास कर सकें: 'intent.setClassName (" com.android.browser "," com.android.browser.BrowserActivity "); ' – Zharf

7

ब्राउज़र केवल HTML और अन्य संगत फ़ाइलों के लिए शुरू किया गया है। यह काम करना चाहिए:

Intent intent = new Intent(ACTION_VIEW); 
intent.setDataAndType(Uri.fromFile(file), "text/html"); 
+0

यह भी काम करता है। धन्यवाद! – roose

+0

बढ़िया! यह एक जादू की तरह काम करता है :) – evya

1

हो सकता है कि यह काम करता है:

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.fromFile(file), "text/html"); 
startActivity(intent); 
6

यह वही मेरे लिए काम कर रहा है। मैंने एंड्रॉइड के डिफ़ॉल्ट ब्राउज़र के Manifest.xml से सीधे माइम प्रकार लिया। जाहिर है पाठ/एचटीएमएल माइम केवल http (एस) और इनलाइन योजनाओं के लिए है।

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.addCategory(Intent.CATEGORY_BROWSABLE); 
intent.setDataAndType(Uri.fromFile(filePath), "application/x-webarchive-xml"); 
startActivity(intent); 

सुनिश्चित नहीं हैं कि अगर यह हर एंड्रॉयड/फोन/ब्राउज़र संयोजन में काम करता है, लेकिन यह एक ही तरीका है कि मैं यह काम करने के लिए मिल सकता है है।

संपादित करें: क्रोम के साथ परीक्षण किया गया और काम नहीं करता है। मेरे 2.3.3 डिवाइस के साथ भी काम नहीं करता है। एंड्रॉइड 4.0 में डिफ़ॉल्ट ब्राउज़र के साथ काम करने लगता है।

0

समस्या यह है कि नई गतिविधि के पास आपके ऐप के अंदर HTML पृष्ठ तक कोई पहुंच नहीं है क्योंकि यह एक अलग ऐप है और इसके पास ऐसा करने की कोई अनुमति नहीं है।

private void openInBrowser(File file) { 
    final Uri uri = Uri.fromFile(file); 
    try { 
     final Intent browserIntent = new Intent(Intent.ACTION_VIEW); 
     browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity"); 
     browserIntent.setData(uri); 
     startActivity(browserIntent); 
    } catch (ActivityNotFoundException e) { 
     final Intent browserIntent = new Intent(Intent.ACTION_VIEW); 
     browserIntent.setDataAndType(Uri.fromFile(file), "text/html"); 
     startActivity(browserIntent); 
    } 
} 

मैं एक नेक्सस वन (16 एपीआई पर इस परीक्षण किया है, 4.1:

0

इस कोड को दोनों एपीआई 10 और एपीआई 11.

File f = new File("/mnt/sdcard/index.html"); 

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.addCategory(Intent.CATEGORY_BROWSABLE); 
intent.setDataAndType(Uri.fromFile(f), "application/x-webarchive-xml"); 
// Have to add this one in order to work on Target 2.3.3 (API 10) 
intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity"); 
startActivity(intent); 
2

यहाँ एक slighly अधिक मजबूत तरीका है पर काम करता है .2), जहां try काम करता है, और एक नेक्सस 5 (एपीआई 22, 5.1.1), जहां केवल catch काम करता है।

संबंधित मुद्दे

 संबंधित मुद्दे