2013-12-16 6 views
8

का उपयोग कर सर्वर से पीडीएफ फ़ाइल खोलना मैं वेब पर खोज रहा हूं कि एंड्रॉइड पर डिफ़ॉल्ट पीडीएफ दर्शक के साथ सर्वर से पीडीएफ फ़ाइल कैसे खोलें। जो मैंने पाया वह फ़ाइल को पहले डाउनलोड करें, फिर इसे इरादे से शुरू करें या इसे Google डॉक्स से लोड करें। मैं ये सब नहीं करना चाहता। मैं सिर्फ फोन से डिफ़ॉल्ट पीडीएफ दर्शक में सीधे सर्वर से लोड करना चाहता हूं। मैंने इरादे से वीडियो यूआरएल खोलने की कोशिश की है और यह काम किया है। लेकिन इरादे से पीडीएफ यूआरएल खोलना काम नहीं कर रहा है। नीचे मेरा कोड है;एंड्रॉइड इरादे

private void openFilePDF(){ 
     try{ 
      Toast.makeText(getBaseContext(), "Opening PDF... ", Toast.LENGTH_SHORT).show(); 
      Intent inte = new Intent(Intent.ACTION_VIEW); 
      inte.setDataAndType(
        Uri.parse("http://122.248.233.68/pvfiles/Guide-2.pdf"), 
        "application/pdf"); 

      startActivity(inte); 
      }catch(ActivityNotFoundException e){ 
       Log.e("Viewer not installed on your device.", e.getMessage()); 
      } 
    } 

क्या कोई तरीका है कि मैं इरादे में पीडीएफ यूआरएल लोड कर सकता हूं?

+1

http://stackoverflow.com/questions/2883355/how खुलेगा -to-render-pdf-in-android/ – Raghunandan

+1

मैं डाउनलोड नहीं करना चाहता, बस इसे सर्वर से लोड करना चाहता हूं। – ksh

+0

http://stackoverflow.com/questions/24740228/android-download-pdf-from-url-then-open-it-with-a-pdf-reader संदर्भ टॉम के लिए यह लिंक देखें फ़ाइल डाउनलोड करें –

उत्तर

16

पहले एक डाउनलोडर वर्ग

public class Downloader { 

    public static void DownloadFile(String fileURL, File directory) { 
     try { 

      FileOutputStream f = new FileOutputStream(directory); 
      URL u = new URL(fileURL); 
      HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
      c.setRequestMethod("GET"); 
      c.setDoOutput(true); 
      c.connect(); 

      InputStream in = c.getInputStream(); 

      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      while ((len1 = in.read(buffer)) > 0) { 
       f.write(buffer, 0, len1); 
      } 
      f.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 

उसके बाद बनाएं एक गतिविधि है जो इंटरनेट से पीडीएफ फाइल डाउनलोड करता है बनाने के लिए,

public class MainActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     String extStorageDirectory = Environment.getExternalStorageDirectory() 
     .toString(); 
     File folder = new File(extStorageDirectory, "pdf"); 
     folder.mkdir(); 
     File file = new File(folder, "Read.pdf"); 
     try { 
      file.createNewFile(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     Downloader.DownloadFile("http://122.248.233.68/pvfiles/Guide-2.pdf", file); 

     showPdf(); 
    } 
    public void showPdf() 
     { 
      File file = new File(Environment.getExternalStorageDirectory()+"/Mypdf/Read.pdf"); 
      PackageManager packageManager = getPackageManager(); 
      Intent testIntent = new Intent(Intent.ACTION_VIEW); 
      testIntent.setType("application/pdf"); 
      List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY); 
      Intent intent = new Intent(); 
      intent.setAction(Intent.ACTION_VIEW); 
      Uri uri = Uri.fromFile(file); 
      intent.setDataAndType(uri, "application/pdf"); 
      startActivity(intent); 
     } 
} 

अंत में पिछले persmissions AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

आपकी जानकारी के लिए धन्यवाद । मैं कोशिश करूँगा। – ksh

+0

@ksh क्या आपकी समस्या हल हो गई है? क्या किसी भी उत्तर ने आपकी मदद की है या नहीं? – GrIsHu

+0

क्या यह काम करता है? –

8

में घोषित आप WebView का उपयोग करके इसे आजमा सकते हैं:

public class MyPdfViewActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    WebView mWebView=new WebView(MyPdfViewActivity.this); 
    mWebView.getSettings().setJavaScriptEnabled(true); 
    mWebView.getSettings().setPluginsEnabled(true); 
    mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+LinkTo); 
    setContentView(mWebView); 
    } 
} 
+0

मुझे पता है यह वास्तव में पुराना है, लेकिन पूर्णता के लिए 'setPluginsEnabled() 'एपीआई स्तर 9 में बहिष्कृत किया गया था, और एपीआई स्तर 18 में हटा दिया गया था। नया संस्करण' setPluginState (WebSettings.PluginState.ON) 'को एपीआई स्तर 8 में जोड़ा गया था लेकिन API में बहिष्कृत किया गया था स्तर 18. प्लगइन एपीआई स्तर 18 से परे समर्थित नहीं हैं। –

+0

@LeeRichardson आपकी टिप्पणी के लिए धन्यवाद, आप इसके साथ उत्तर अपडेट कर सकते हैं .. –

+0

अपने डाउनलोड करने योग्य पीडीएफ फ़ाइल यूआरएल को "https://docs.google.com/gview?embedded=true&url=" के साथ बांधना सबसे महत्वपूर्ण है। यह .. – CrazyMind

0

मेरे अनुसार डिवाइस पर अपनी पीडीएफ फ़ाइल को सीधे खोलने का कोई तरीका नहीं है। जब हम पीडीएफ फ़ाइल खोलने का प्रयास करते हैं तो यह एंड्रॉइड उपकरणों के ब्राउज़र गुणों के कारण डिवाइस पर डाउनलोड होता है। पीडीएफ फ़ाइल खोलने के लिए केवल दो ही तरीके हैं।

  1. आप फ़ाइल खोलने के लिए ऐप चुनने के लिए पीडीएफ एप्लिकेशन इरादे का उपयोग कर सकते हैं।

  2. आप गूगल डॉक्स यूआरएल के साथ फ़ाइल के लिए अपने सर्वर यूआरएल जोड़ सकते हैं और ब्राउज़र में खोल सकते हैं तो उर पीडीएफ फाइल ब्राउज़र में

+0

आपके उत्तर के लिए धन्यवाद। लेकिन पीडीएफ ऐप का इरादा केवल स्थानीय फाइलें खोल सकता है। यह सर्वर से फ़ाइलों को लोड नहीं कर सकता है। – ksh

+0

हाँ आपको डिवाइस पर फ़ाइल डाउनलोड करनी है और इसके बाद आप उस विशेष फ़ाइल को खोलने के इरादे का उपयोग कर सकते हैं –

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