2012-04-20 42 views

Trả lời

0

bạn có thể sao chép tệp từ tài nguyên thô sang sdcard, sau đó gọi startActivity() trên ACTION_VIEW Intent có URI trỏ tới bản sao có thể đọc được và cũng có loại MIME thích hợp.

Tất nhiên, thao tác này sẽ chỉ hoạt động trên thiết bị có trình xem tài liệu Word trên đó.

26

Không giống như iOS, bản thân Android không hỗ trợ hiển thị các tệp .doc hoặc .ppt. Bạn đang tìm kiếm mục đích công khai cho phép ứng dụng của bạn sử dụng lại các hoạt động của các ứng dụng khác để hiển thị các loại tài liệu này. Nhưng điều này sẽ chỉ hoạt động đối với điện thoại có cài đặt ứng dụng hỗ trợ Mục đích này.

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

hoặc nếu bạn đã cài đặt một số ứng dụng sau đó sử dụng Ý định này:

//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

Làm việc như một sự quyến rũ. –

11

Dưới đây là một phương pháp để chăm sóc này cho bạn:

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:")); 
} 
9

Open Document từ Danh sách các ứng dụng sẵn có Người dùng phải chọn ứng dụng từ danh sách ứng dụng

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

Vậy thì, 'request_code' trong' startActivityForResult() 'là gì. DOC không đại diện cho một mã yêu cầu cụ thể. – CagCak

3

bạn có thể mở tệp trong chế độ xem web nếu bạn muốn mở trong ứng dụng. ví dụ:

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

Tôi đang cố gắng xem những tùy chọn nào tôi đã tạo để xem trước tệp, không biết bạn có thể gửi cho Google tài liệu URL không! –

0

Dưới đây là cách hoàn chỉnh để mở file .doc trong Android 7.0 trở xuống:

Bước 1: Trước hết, đặt tập tin pdf của bạn vào tài sản thư mục như ảnh chụp màn hình sau. Placing doc file in assets folder

Bước 2: Bây giờ đi đến build.gradle tập tin và thêm dòng sau:

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

}

và sau đó dưới sự phụ thuộc thêm dòng và đồng bộ sau:

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

Bước 3: Bây giờ, hãy thêm một tệp java mới có thể mở rộng từ FileProvider Giống như tên tệp của tôi là LegacyCompatFileProvider và mã bên trong nó.

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))); 
    } 
} 

Bước-4: Tạo một thư mục có tên "xml" dưới "res" thư mục. (Nếu thư mục đã có thì không cần phải tạo). Bây giờ, hãy thêm tệp providers_path.xml, trong thư mục xml.Dưới đây là ảnh chụp màn hình: dòng Place of provider_path xml file

Bên trong tập tin add sau:

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

Bước-5: Bây giờ đi đến AndroidManifest.xml tập tin và dòng sau trong <application></application> tag:

<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> 

Bước 6: Bây giờ hãy chuyển đến lớp Hoạt động từ wh ere bạn muốn tải pdf và thêm sau 1 dòng và 2 phương pháp:

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(); 
    } 

Bây giờ gọi LoadPdfFile phương pháp và thông qua tên tập tin của bạn mà không .doc như trong trường hợp của tôi "chapter-0" và nó sẽ mở file doc trong ứng dụng đọc doc.

Các vấn đề liên quan