android开发调用系统相机选择图片

释放双眼,带上耳机,听听看~!
package com.ifreshwing.speedshooting;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class ProductInfoActivity extends BaseActivity {
private static final int IMAGE = 1;//调用系统相册-选择图片
private TextView tv_slt;
private ImageView iv_slt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_info);
initview();
setToolbar_string("产品属性");
setToolbar_left_tv("返回");
setToolbar_right_tv("完成");
getToolbar_right_tv().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void initview() {
tv_slt = (TextView) findViewById(R.id.tv_slt);
iv_slt = (ImageView) findViewById(R.id.iv_slt);
tv_slt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE);
//                tv_slt.setVisibility(View.GONE);
iv_slt.setVisibility(View.VISIBLE);
tv_slt.setVisibility(View.GONE);
}
});
iv_slt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE);
//                tv_slt.setVisibility(View.GONE);
iv_slt.setVisibility(View.VISIBLE);
tv_slt.setVisibility(View.GONE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//获取图片路径
if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumns = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePathColumns[0]);
String imagePath = c.getString(columnIndex);
showImage(imagePath);
c.close();
}
}
//加载图片
private void showImage(String imaePath){
Bitmap bm = BitmapFactory.decodeFile(imaePath);
((ImageView)findViewById(R.id.iv_slt)).setImageBitmap(bm);
}
}

重要的代码是绿色的代码

人已赞赏
Android文章

Android开发CoordinatorLayout + AppBarLayout+GridView

2020-1-31 19:55:56

Android文章

Android开发图片变灰的方法

2020-1-31 20:13:39

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索