2012-04-20 14 views
12

क्या कोई .doc एक्सटेंशन फ़ाइल खोलने का कोई संभावित तरीका है?एंड्रॉइड एक .doc एक्सटेंशन फ़ाइल कैसे खोलें?

+1

फ़ाइल खोलकर आपका क्या मतलब है। क्या आप फ़ाइल देखना चाहते हैं या फ़ाइल की सामग्री का उपयोग करना चाहते हैं। – MrWaqasAhmed

+0

मेरे ऐप में देखने के लिए – Narendra

+0

ओपन एमएस शब्द, एक्सेल, पीडीएफ फ़ाइल व्यूअर – Dinesh

उत्तर

0

आप कच्चे संसाधन से sdcard में फ़ाइल की प्रतिलिपि बना सकते हैं, फिर ACTION_VIEW इरादे पर startActivity() को कॉल करें जिसमें यूरी पठनीय प्रति को इंगित करती है और इसमें उचित MIME प्रकार भी है।

बेशक, यह केवल उस डिवाइस पर काम करेगा जिसमें वर्ड डॉक्यूमेंट व्यूअर है।

26

आईओएस के विपरीत, एंड्रॉइड स्वयं प्रतिपादन .doc या .ppt फ़ाइलों का समर्थन नहीं करता है। आप एक सार्वजनिक मंशा की तलाश में हैं जो आपके ऐप को इन दस्तावेज़ प्रकारों को प्रदर्शित करने के लिए अन्य ऐप्स की गतिविधियों का पुन: उपयोग करने की अनुमति देता है। लेकिन यह केवल उस फोन के लिए काम करेगा जिसमें एक ऐप स्थापित है जो इस इरादे का समर्थन करता है।

http://developer.android.com/guide/topics/intents/intents-filters.html

या आपके द्वारा स्थापित करता है, तो कुछ एप्लिकेशन तो इस आशय का उपयोग करें: से

public void openDocument(String name) { 
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
    File file = new File(name); 
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()); 
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); 
    if (extension.equalsIgnoreCase("") || mimetype == null) { 
     // if there is no extension or there is no definite mimetype, still try to open the file 
     intent.setDataAndType(Uri.fromFile(file), "text/*"); 
    } else { 
     intent.setDataAndType(Uri.fromFile(file), mimetype);    
    } 
    // custom message for the intent 
    startActivity(Intent.createChooser(intent, "Choose an Application:")); 
} 
+0

एक आकर्षण की तरह काम किया। –

11

यहाँ आप के लिए इस की देखभाल के लिए एक विधि है उपलब्ध अनुप्रयोग उपयोगकर्ता को आवेदन की सूची से आवेदन चुनना है

File targetFile = new File(path); 
        Uri targetUri = Uri.fromFile(targetFile); 
        Intent intent = new Intent(Intent.ACTION_VIEW); 
        intent.setDataAndType(targetUri, "application/*"); 
        startActivityForResult(intent, DOC); 
9

ओपन दस्तावेज़:

//Uri uri = Uri.parse("file://"+file.getAbsolutePath()); 
Intent intent = new Intent(); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.setAction(Intent.ACTION_VIEW); 
String type = "application/msword"; 
intent.setDataAndType(Uri.fromFile(file), type); 
startActivity(intent); 
+0

तो फिर, startActivityForResult() 'में' request_code' क्या है। डीओसी एक विशिष्ट अनुरोध कोड का प्रतिनिधित्व नहीं करता है। – CagCak

3

यदि आप ऐप के भीतर खोलना चाहते हैं तो आप वेबव्यू में फ़ाइल खोल सकते हैं। पूर्व:

String doc="<iframe src='http://docs.google.com/viewer? url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true'"+ 
    " width='100%' height='100%' style='border: none;'></iframe>"; 

     WebView wv = (WebView)findViewById(R.id.fileWebView); 
     wv.getSettings().setJavaScriptEnabled(true); 
     wv.getSettings().setAllowFileAccess(true); 
     //wv.loadUrl(doc); 
     wv.loadData(doc , "text/html", "UTF-8"); 
+0

मैं यह देखने का प्रयास कर रहा था कि मुझे फ़ाइल पूर्वावलोकन बनाने के लिए कौन से विकल्प बनाना था, आपको नहीं पता था कि आप Google दस्तावेज़ों को एक यूआरएल भेज सकते हैं! –

0

यहां Android 7.0 या में .doc फ़ाइल को खोलने के लिए पूरा तरीका है कम:

चरण -1: सबसे पहले, अपने pdf फ़ाइल जगह संपत्ति की तरह फ़ोल्डर में निम्नलिखित स्क्रीनशॉट। Placing doc file in assets folder

चरण -2: अब फ़ाइल build.gradle के पास जाकर जोड़ने निम्नलिखित लाइनों:

repositories { 
maven { 
    url "https://s3.amazonaws.com/repo.commonsware.com" 
} 

}

और फिर निर्भरता के तहत निम्न पंक्ति और सिंक जोड़ें:

compile 'com.commonsware.cwac:provider:0.4.3' 

चरण -3: अब एक नई जावा फ़ाइल जोड़ें जो FileProvider से बढ़ाना चाहिए मेरे मामले में फ़ाइल नाम LegacyCompatFileProvider और उसके अंदर कोड है।

import android.database.Cursor; 
import android.net.Uri; 

import android.support.v4.content.FileProvider; 

import com.commonsware.cwac.provider.LegacyCompatCursorWrapper; 

public class LegacyCompatFileProvider extends FileProvider { 
    @Override 
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 
    return(new LegacyCompatCursorWrapper(super.query(uri, projection, selection, selectionArgs, sortOrder))); 
    } 
} 

चरण -4: "res" फ़ोल्डर के अंतर्गत "xml" नाम का एक फ़ोल्डर बनाएँ। (यदि फ़ोल्डर पहले से मौजूद है तो बनाने की कोई आवश्यकता नहीं है)। xml फ़ोल्डर में अब providers_path.xml फ़ाइल जोड़ें। Place of provider_path xml file

फ़ाइल जोड़ने के अंदर निम्नलिखित लाइनों:

<?xml version="1.0" encoding="utf-8"?> 
<paths> 
    <files-path name="stuff" /> 
</paths> 

चरण -5: अब <application></application> टैग में AndroidManifest.xml फ़ाइल और निम्नलिखित लाइनों पर जाएँ:

<provider 
      android:name="LegacyCompatFileProvider" 
      android:authorities="REPLACE_IT_WITH_PACKAGE_NAME" 
      android:exported="false" 
      android:grantUriPermissions="true"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/provider_paths"/> 
     </provider> 

यहाँ स्क्रीनशॉट है चरण -6: अब से गतिविधि कक्षा में जाएं

private static final String AUTHORITY="REPLACE_IT_WITH_PACKAGE_NAME"; 

static private void copy(InputStream in, File dst) throws IOException { 
     FileOutputStream out=new FileOutputStream(dst); 
     byte[] buf=new byte[1024]; 
     int len; 

     while ((len=in.read(buf)) > 0) { 
      out.write(buf, 0, len); 
     } 

     in.close(); 
     out.close(); 
    } 

    private void LoadPdfFile(String fileName){ 

     File f = new File(getFilesDir(), fileName + ".doc"); 

     if (!f.exists()) { 
      AssetManager assets=getAssets(); 

      try { 
       copy(assets.open(fileName + ".doc"), f); 
      } 
      catch (IOException e) { 
       Log.e("FileProvider", "Exception copying from assets", e); 
      } 
     } 

     Intent i= 
       new Intent(Intent.ACTION_VIEW, 
         FileProvider.getUriForFile(this, AUTHORITY, f)); 

     i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

     startActivity(i); 
     finish(); 
    } 

अब LoadPdfFile विधि कॉल और मेरे मामले "chapter-0" में तरह .doc के बिना अपने फ़ाइल नाम गुजरती हैं और यह दस्तावेज़ पाठक आवेदन में दस्तावेज़ फ़ाइल खुलेगी: पहले आप पीडीएफ लोड और 1 लाइन और इन 2 तरीकों का पालन जोड़ना चाहते हैं।

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