private Button mGetImg;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
}
public void findView() {
mGetImg = (Button) findViewById(R.id.mGetLocalImg);
mGetImg.setOnClickListener(this);
}
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
// intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1:
Uri uri = data.getData();
ContentResolver cr = getContentResolver();
Cursor mCur = cr.query(uri, null, null, null, null);
/** 得到本地图片库中图片的 id、路径、大小、文件名 */
// cursor.getString(0),1,2,3
if(!mCur.moveToFirst()){
return;
}
try {
String mImgPath = mCur.getString(mCur.getColumnIndex("_data"));
System.out.println("mImgPath__________" + mImgPath);
// InputStream in = cr.openInputStream(uri);
// Bitmap mBitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
应用获取手机本地图片资源
最新推荐文章于 2021-11-13 17:26:29 发布