2014-01-23 89 views
24

我正在嘗試一個示例程序來在內部存儲中存儲文件,並使用 Intent.ACTION_VIEW打開它。如何使用Intent.ACTION_VIEW打開保存到內部存儲的私人文件?

對於文件存儲在私密模式我遵循的步驟提供here.

我能找到在內部存儲在/data/data/com.storeInternal.poc/files創建的文件。*

但是,當我試圖打開文件,它不會打開。

請在下面找到我使用它的代碼。

public class InternalStoragePOCActivity extends Activity { 
    /** Called when the activity is first created. */ 
    String FILENAME = "hello_file.txt"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     createFile(); 
     openFile(FILENAME); 
    } 

    public FileOutputStream getStream(String path) throws FileNotFoundException { 
     return openFileOutput(path, Context.MODE_PRIVATE); 
    } 

    public void createFile(){ 

     String string = "hello world!"; 
     FileOutputStream fout = null; 
     try { 
      //getting output stream 
      fout = getStream(FILENAME); 
      //writng data 
      fout.write(string.getBytes()); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }finally{ 
      if(fout!=null){ 
       //closing the output stream 
       try { 
        fout.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

     } 

    } 

    public void openFile(String filePath) { 
     try { 
      File temp_file = new File(filePath); 

      Uri data = Uri.fromFile(temp_file); 
      String type = getMimeType(data.toString()); 

      Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
      intent.setDataAndType(data, type); 
      startActivity(intent); 

     } catch (Exception e) { 
      Log.d("Internal Storage POC ", "No Supported Application found to open this file"); 
      e.printStackTrace(); 

     } 
    } 

    public static String getMimeType(String url) { 
     String type = null; 
     String extension = MimeTypeMap.getFileExtensionFromUrl(url); 

     if (extension != null) { 
      MimeTypeMap mime = MimeTypeMap.getSingleton(); 
      type = mime.getMimeTypeFromExtension(extension); 
     } 

     return type; 
    } 
} 

我如何可以打開使用Context.MODE_PRIVATE通過任何其他現有/適當應用存儲​​的文件。例如:file.pdf應該由PDF閱讀器,視頻播放器等打開。

+0

在這裏看到我的回答:http://stackoverflow.com/questions/15377373/how-to-put-a-video -file-in-android-custom-content-provider – dangalg

回答

46

您不能在URI引用的內部存儲中通過Intent將文件共享/發送到其他應用程序。應用程序無法讀取其他應用程序的私人數據(除非它是通過內容提供商)。您在意圖中傳遞文件的URI(而不是實際的文件本身),並且接收意圖的應用程序需要能夠從該URI讀取。

最簡單的解決方案是先將文件複製到外部存儲並從那裏共享。如果您不想這樣做,請創建一個Content Provider來公開您的文件。一個例子可以在這裏找到:Create and Share a File from Internal Storage

編輯:要使用內容提供商:

首先如下創建內容提供商類。這裏重要的覆蓋是'openFile'。當使用文件URI調用內容提供者時,此方法將運行併爲其返回一個ParcelFileDescriptor。其他方法也需要存在,因爲它們在ContentProvider中是抽象的。

public class MyProvider extends ContentProvider { 

@Override 
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { 
    File privateFile = new File(getContext().getFilesDir(), uri.getPath()); 
    return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY); 
} 

@Override 
public int delete(Uri arg0, String arg1, String[] arg2) { 
    return 0; 
} 

@Override 
public String getType(Uri arg0) { 
    return null; 
} 

@Override 
public Uri insert(Uri arg0, ContentValues arg1) { 
    return null; 
} 

@Override 
public boolean onCreate() { 
    return false; 
} 

@Override 
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3, 
     String arg4) { 
    return null; 
} 

@Override 
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) { 
    return 0; 
} 
} 

應用標籤內定義您的清單供應商,出口= true,以允許其他應用程序使用它:

<provider android:name=".MyProvider" android:authorities="your.package.name" android:exported="true" /> 

在你在你的活動有中openFile()方法,設置建立一個URI如下。這個URI指向您的包裹內容提供商與文件名一起:

Uri uri = Uri.parse("content://your.package.name/" + filePath); 

最後,設置此URI的意圖:

intent.setDataAndType(uri, type); 

記住插入自己的包名,我已經使用「上面的your.package.name'。

+0

我將如何使用內容提供程序進行最少更改? – Abhinav

+0

請參閱我上面的修改。 – NigelK

+0

工作就像魅力!謝謝。 –

0
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() 
     + "Foder path" + "/" + "File Name"); 

     Uri uri = Uri.fromFile(file); 
     Intent in = new Intent(Intent.ACTION_VIEW); 
     in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     in.setDataAndType(uri, "mime type"); 
     startActivity(in); 

[注:請選擇MIME類型按文件類型]

0

如果有人仍然面臨着類似的問題:該文件無法訪問。這可能有助於你。與內容提供商一起,您還必須設置以下標誌......

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

intent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); 

我需要的聲譽PLZ:d