在Android应用程序中附加任何类型的文件? [英] Attaching a file of any type in Android application?
问题描述
我正在构建一个应用程序,其中当用户按下应用程序上的浏览"按钮时,我想显示手机中的文件夹,用户可以在其中选择文件/图像.选择附件后,应用程序将在附件中显示文件名.我正在寻找的是文件附件机制在android中的工作方式,因此非常感谢任何代码示例或摘要.
I am building an application in which when a user press "Browse" button on the application I want to show folders from phone where user will select the file/image. Once the attachment is selected, application will show the file name in the attachments. What I am looking for is how the file attachment mechanism work in android so any code example or snippet in much appreciated.
谢谢.
推荐答案
您实际要做的是执行Intent.ACTION_GET_CONTENT.如果将类型指定为"file/*"
,则文件选择器将允许您从文件系统中选择任何类型的文件.
What you actually want to do is execute Intent.ACTION_GET_CONTENT. If you specify type to be "file/*"
then the file picker will allow you to select file of any type from the file system.
以下是一些读物:
- Android documentation on ACTION_GET_CONTENT
- A working example
这是从博客中提取的信息(由Android-er提供):
This is the extracted source from the blog (courtesy of Android-er):
public class AndroidPick_a_File extends Activity {
TextView textFile;
private static final int PICKFILE_RESULT_CODE = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonPick = (Button)findViewById(R.id.buttonpick);
textFile = (TextView)findViewById(R.id.textfile);
buttonPick.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
}});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(requestCode){
case PICKFILE_RESULT_CODE:
if(resultCode==RESULT_OK){
String FilePath = data.getData().getPath();
textFile.setText(FilePath);
}
break;
}
}
}
在获取选定文件的路径之后,就由您决定如何处理它:将路径存储在数据库中,在屏幕上显示等等.
After acquiring path to the selected file it is up to you on how are you going to handle it: store path in a database, display it on screen, etc.
如果要使用默认应用程序打开文件,请遵循此博客.再次,我提取了代码(由Hello World Codes提供):
If you want to open the file with a default application, follow advices in this blog. Again, I extracted the code (courtesy of Hello World Codes):
第一种方式:
String path="File Path";
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
intent.setData(Uri.fromFile(file));
startActivity(intent);
第二种方式:
String path="File Path";
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=file.getName().substring(file.getName().indexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(file),type);
startActivity(intent);
请记住感谢那些应得的人(-.
Please remember to leave thanks to the guys who deserve it (-.
- http://android-er. blogspot.co.uk/2011/03/pick-file-using-intentactiongetcontent.html
- http://helloworldcodes.blogspot. co.uk/2011/10/android-open-folder-with-default.html
- http://android-er.blogspot.co.uk/2011/03/pick-file-using-intentactiongetcontent.html
- http://helloworldcodes.blogspot.co.uk/2011/10/android-open-folder-with-default.html
这篇关于在Android应用程序中附加任何类型的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!