我正在尝试一个示例程序来将文件存储在内部存储器中,然后使用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阅读器打开,视频应该由视频播放器打开,等等。
您无法通过Intent共享或发送内部存储中的文件(如URI所指),该Intent会传递给另一个应用。一个应用程序不能读取另一个应用程序的私有数据(除非它是通过内容提供者提供的)。您在意图中传递文件的URI(而不是实际文件本身),并且接收意图的应用程序必须能够从该URI读取。
最简单的解决方案是先将文件复制到外部存储,然后从那里共享。如果您不想这样做,请创建一个内容提供程序以公开您的文件。可以在此处找到一个示例:从内部存储创建和共享文件
编辑:要使用内容提供程序:
首先,如下创建您的内容提供商类。这里重要的替代方法是'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;
}
}
在application标记中的清单中定义您的提供者,并使用export = 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”中插入您自己的软件包名称。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句