嗨,Android 开发新手。当方向改变时需要帮助维护 imageView 中的图像。此时,当我拍照或上传照片时,它会很好地上传,直到我切换方向。我尝试从其他帖子中搜索不太了解。我想这与保存实例有关。有人可以帮我解决这个问题。
img1.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
Log.d("take","pic");
if (resultCode == -1) {
String encodedImageString = null;
Uri selectimage=intent.getData();
Log.d("take","picture");
ImageView img1=(ImageView)findViewById(R.id.img1);
//Changing URI to Bitmap
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectimage));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Reducing Memory
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bmp.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");
}
//setting Imageview as the bitmap so could send it to the canvas
img1.setImageBitmap(bmp);
}
break;
case 1://Selecting from Gallery
Log.d("view","pic");
if (resultCode == -1) {
String encodedImageString = null;
Uri selectimage = intent.getData();
String selectedImagepath = getPath(selectimage);
ImageView img1=(ImageView)findViewById(R.id.img1);
//Changing URI to Bitmap
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectimage));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//Reducing Memory
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bmp.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");
}
Log.d("view","picture");
//setting Imageview as the bitmap so could send it to the canvas
img1.setImageBitmap(bmp);
}
break;
}
}