public class MainActivity extends Activity {
Button bn;
int REQUEST_CODE = 1;
ImageView IMG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bn = (Button) findViewById(R.id.bn);
IMG = (ImageView) findViewById(R.id.img);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(i.resolveActivity(getPackageManager())!= null){
startActivityForResult(i,REQUEST_CODE);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onActivityResult(int requestcode, int resultcode, Intent data){
if(requestcode == REQUEST_CODE && resultcode == RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
IMG.setImageBitmap(imageBitmap);
}
}
}
答案 0 :(得分:0)
嗨我想你要从画廊或相机拍照,然后你想把那个图像设置为ImageView。如果我是对的,请按照以下代码
private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
String currentTimeStamp = dateFormat.format(new Date());
f = new File(android.os.Environment.getExternalStorageDirectory(), currentTimeStamp);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}
private void openGallery() {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select File"), REQUEST_GALLERY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
try {
String click = f.getAbsolutePath();
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(User_Detail.KEY_ALBUM_IMAGE_PATH, click);
values.put(User_Detail.USER_ID, Utlity.user_id);
values.put(User_Detail.KEY_ID_ALBUM_REF, album_id);
long loginStat = db.insert(User_Detail.TABLE_4, null, values);
loadData(album_id);
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
picturePath = c.getString(columnIndex);
if (picturePath != null && picturePath.length() > 1) {
ContentValues va = new ContentValues();
} else {
}
SQLiteDatabase db = dbHelper.getWritableDatabase();
String image_path = picturePath;
ContentValues values = new ContentValues();
values.put(User_Detail.KEY_ALBUM_IMAGE_PATH, image_path);
values.put(User_Detail.USER_ID, Utlity.user_id);
values.put(User_Detail.KEY_ID_ALBUM_REF, album_id);
long loginStat = db.insert(User_Detail.TABLE_4, null, values);
loadData(album_id);
c.close();
Log.w("path of image from gallery......******************.........", picturePath + "");
}
}
}
现在您获得了选择图像或捕获图像的ImagePath。现在你需要在你的Image View中设置Crate a Adapter类,它扩展了BaseAdapter并在那个适配器类中你可以通过将Imagepath转换为Bitmap来设置这个图像