我有点沮丧试图让它发挥作用。有人可以帮我解决这个问题。我是新手,我想要的只是从 Gallery/Image_capture 获取图像以始终显示在 ImageView 上。我和很多人来来回回,得到了不同的答案。
这是具有名为 editpic 的 ImageView 的 Activity。一旦用户点击它,它会打开一个对话框,要求从画廊或相机中进行选择。
public int GET_CAM_IMG=2;
public int GET_GAL_IMG=1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
profile();
private void profile() {
editpic = (ImageView)findViewById(R.id.editpic);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v){
CharSequence[] names = { "From Gallery", "From Camera" };
new AlertDialog.Builder(context)
.setTitle("Select an option for updating your Profile Picture")
.setItems(names, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int pos) {
// TODO Auto-generated method stub
if (pos == 0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, GET_GAL_IMG);
} else {
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, GET_CAM_IMG);
}
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
}
}).create().show();
}});
一旦用户点击它就会通过机箱和开关
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case 2://Camera
if (resultCode == -1) {
String encodedImageString = null;
Uri selectedImage = intent.getData();
String selectedImagepath = getPath(selectedImage);
editpic.setImageURI(selectedImage);
Bitmap bmp_image = BitmapFactory.decodeFile(filePath);
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bmp_image.compress(Bitmap.CompressFormat.JPEG, 50, baos)) {
byte[] image = baos.toByteArray();
encodedImageString = Base64.encodeToString(image,
Base64.DEFAULT);
} else {
System.out.println("Compreesion returned false");
Log.d("Compress", "Compreesion returned false");
}
break;
case 3://Selecting from Gallery
if (resultCode == -1) {
String encodedImageString = null;
Bitmap bmp_image = null;
Bundle extras = intent.getExtras();
bmp_image = (Bitmap) extras.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bmp_image.compress(Bitmap.CompressFormat.JPEG, 50, baos)) {
byte[] image = baos.toByteArray();
encodedImageString = Base64.encodeToString(image,
Base64.DEFAULT);
} else {
System.out.println("Compression returned false");
Log.d("Compress", "Compression returned false");
}
}
break;
}
}
尝试创建一种方法,以便我可以获取图像路径,然后使用它,但无法让它工作
SharedPreferences userprofile = getSharedPreferences(filename,0);
SharedPreferences.Editor editor = userprofile.edit();
editor.putString("Imagepath",selectedImagepath);
editor.commit();
拍照后,图像出现在 Imageview 上,但消失了。我希望它在那里,因为这是个人资料图片。它应该是电话簿中的联系人图片。