Android文件选择器,android 文件选择
时间:2025-03-12 17:06:08
作者:兔宝宝游戏网
浏览:7
android 文件选择,最近不少朋友在找android 文件选择的相关介绍,兔宝宝游戏网给大家详细的介绍一下,希望对大家有帮助。
1、Android文件选择器:
privatevoidopenFileBrowse(){
Intentintent=newIntent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");//文件类型
IntentwrapperIntent=Intent.createChooser(intent,"打开方式");
startActivityForResult(wrapperIntent,0);
}
protectedvoidonActivityResult(intrequestCode,intresultCode,
Intentintent){
//Stringpath;
if(resultCode==RESULT_OK){
if(requestCode==0){
if(intent==null){
Toast.makeText(this,"未选择任何文件",Toast.LENGTH_LONG).show();
return;
}
Uriuri=intent.getData();
if(uri==null){
Toast.makeText(this,"未选择任何文件",Toast.LENGTH_LONG).show();
return;
}
Stringscheme=uri.getScheme();
//可能是content,file等等.需要判断其是不是file
如果不是调用文件管理器,可能返回其他
if(scheme!=null
&&("file".endsWith(scheme.toLowerCase()))
Stringpath=uri.getPath();//文件路径
}
}
}
2、Android编程 打开本地文件 文件选择器:
布局文件
<?xml version="1.0" encoding="utf-8"?>android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:id="@+id/b01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>android:id="@+id/iv01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
代码
import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Lesson_01_Pic extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.b01);
button.setText("选择图片");
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.e("uri", uri.toString());
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
ImageView imageView = (ImageView) findViewById(R.id.iv01);
/* 将Bitmap设定到ImageView */
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
总结:以上内容就是针对Android文件选择器,android 文件选择的详细介绍,大家可以参考一下。
标题:Android文件选择器,android 文件选择
链接:http://www.qzj2.com/article/32934.html
版权:文章转载自网络,如有侵权,请联系删除!
- 上一篇: 详细介绍一下,什么软件能够让电脑有KTV的效果
- 下一篇: 更改用户账户控制设置进不去,
资讯推荐
更多
gta5武器秘籍大全(完整版)无限子弹,gta5的武器秘籍有哪些
gta5武器秘籍大全,完整版无限子弹,gta5的武器秘籍有哪些,gta5武器全满代码是:IFWEREARCHMAN。其他秘籍:一、武
2025-03-12
热门手游
更多
热门攻略
更多
-
海龟汤题目和答案全套恐怖高难度,细思极恐的冷门海龟汤 2025-03-12
-
女生手抄上林赋是什么梗的含义(为什么抄上林赋得心上人) 2025-03-12
-
侠盗飞车罪恶都市秘籍大全(完整版),侠盗飞车罪恶都市秘籍有哪些呢 2025-03-12
-
gta5武器秘籍大全(完整版)无限子弹,gta5的武器秘籍有哪些 2025-03-12
-
梦幻西游义绝墨魂笔3x攻略(神器义绝墨魂笔之踪攻略) 2025-03-12
-
公交车里抓着摇曳的手环诗情,清晨时见雾青草沾雨露是什么歌 2025-03-12
热游排行榜
更多