Abh*_*nav 27 android android-intent
我正在尝试一个示例程序将文件存储在内部存储中,并使用Intent.ACTION_VIEW打开它.
我能够在/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阅读器,视频播放器等打开.
Nig*_*elK 52
您无法通过Intent将内部存储中的文件(由URI引用)共享/发送到另一个应用程序.应用无法读取其他应用的私有数据(除非是通过内容提供商).您在intent(而不是实际文件本身)中传递文件的URI,并且接收intent的应用程序需要能够从该URI读取.
最简单的解决方案是首先将文件复制到外部存储,然后从那里共享.如果您不想这样做,请创建一个Content Provider来公开您的文件.可以在此处找到一个示例:从内部存储创建和共享文件
编辑:使用内容提供商:
首先创建您的内容提供者类,如下所示.这里重要的覆盖是'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;
}
}
在应用程序标记内的Manifest中定义您的提供程序,使用exported = true以允许其他应用程序使用它:
<provider android:name=".MyProvider" android:authorities="your.package.name" android:exported="true" />
在Activity中的openFile()方法中,设置如下的URI.此URI指向包中的内容提供程序以及文件名:
Uri uri = Uri.parse("content://your.package.name/" + filePath);
最后,在意图中设置这个uri:
intent.setDataAndType(uri, type);
记得在我上面使用'your.package.name'的地方插入你自己的包名.
| 归档时间: | 
 | 
| 查看次数: | 31106 次 | 
| 最近记录: |