如何打开保存到使用Intent.ACTION_VIEW内部存储的私人文件? [英] How to open private files saved to the internal storage using Intent.ACTION_VIEW?

查看:104
本文介绍了如何打开保存到使用Intent.ACTION_VIEW内部存储的私人文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个示例程序来存储文件在内部存储和使用开放它 Intent.ACTION_VIEW。

I am trying a sample program to store a file in the internal storage and the open it using Intent.ACTION_VIEW.

有关存储在文件中的私人模式我也跟着步骤这里提供

For storing the file in private mode I followed the steps provided here.

我能找到在/data/data/com.storeInternal.poc/files内部存储创建的文件。*

I was able to find the created file in the internal storage at /data/data/com.storeInternal.poc/files .*

但是,当我试图打开文件时,它不会打开。

But when I tried to open file,it does not open.

请在下面找到了code我用它。

Please find below the code I used for it.

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阅读器,视频通过视频播放器,等开了。

How may I open the file stored using Context.MODE_PRIVATE by any other existing/appropriate app. Eg: file.pdf should be opened by PDF reader,Video by video Players,etc.

推荐答案

您不能共享/在内部存储发送文件,所引用的URI,通过一个意图到另一个应用程序。一个应用程序无法读取其他应用程序的私有数据(除非它是通过一个内容提供商)。您传入的意图(不是实际的文件本身),并接收该意图需要能够从读取该应用的文件的URI的URI。

You can't share/send a file in internal storage, as referenced by a URI, via an Intent to another app. An app cannot read another app's private data (unless it's through a Content Provider). You pass the URI of the file in the intent (not the actual file itself) and the app that receives the intent needs to be able to read from that URI.

最简单的解决方法是先将该文件复制到外部存储设备,并从那里分享。如果你不想这样做,创建一个内容提供商暴露你的文件。一个例子可以在这里找到:<一href="http://stackoverflow.com/questions/12170386/create-and-share-a-file-from-internal-storage">Create并分享文件内部存储

The simplest solution is to copy the file to external storage first and share it from there. If you don't want to do that, create a Content Provider to expose your file. An example can be found here: Create and Share a File from Internal Storage

编辑:要使用内容提供商:

To use a content provider:

首先创建内容提供商类如下。最重要的替代这里是中openFile。当内容提供商是带一个文件的URI,该方法将运行并返回一个ParcelFileDescriptor它。其它方法需要present,以及因为它们是在抽象的ContentProvider

First create your content provider class as below. The important override here is 'openFile'. When your content provider is called with a file URI, this method will run and return a ParcelFileDescriptor for it. The other methods need to be present as well since they are abstract in 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来允许其他应用程序来使用它:

Define your provider in the Manifest within the application tag, with exported=true to allow other apps to use it:

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

在中openFile()方法,你有你的活动,成立了一个URI,如下。这个URI指向你的包内容提供商以及文件名:

In the openFile() method you have in your Activity, set up a URI as below. This URI points to the content provider in your package along with the filename:

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

最后,设置此URI的意图:

Finally, set this uri in the intent:

intent.setDataAndType(uri, type);

记住要插入的地方我都用'your.package.name'上面你自己的包名。

Remember to insert your own package name where I have used 'your.package.name' above.

这篇关于如何打开保存到使用Intent.ACTION_VIEW内部存储的私人文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆