我已经检查了你的代码,所以我认为你需要像这样替换你的按钮操作
private static final int SELECT_PHOTO = 100
Intent photoPickerIntent = new Intent(
Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
并在您的开始活动结果中给出
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
System.out.println("requestcode" + requestCode + "result code "
+ requestCode + "intentt" + imageReturnedIntent);
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
// InputStream imageStream;
try {
Uri selectedImage = imageReturnedIntent.getData();
Bitmap yourSelectedImage = decodeUri(selectedImage);
// imageStream = getContentResolver().openInputStream(
// selectedImage);
// Bitmap yourSelectedImage = BitmapFactory
// .decodeStream(imageStream);
try {
yourimageview.setImageBitmap(yourSelectedImage);
picArray = convertBitmap(yourSelectedImage);
String imagepath_new = getRealPathFromURI(selectedImage);
System.out.println("gakk" + imagepath_new);
String[] s = imagepath_new.split("/");
System.out.println(s[s.length - 1]);
String imageName1 = s[s.length - 1];
imageName1 = imageName1.replace(" ", "");
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Exception" + e, 1000).show();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
并且还添加这些行以避免在 Activity 中显示图像视图时出现内存泄漏。
private byte[] convertBitmap(Bitmap bm) {
// int bytes = bm.getWidth() * bm.getHeight() * 4; // calculate how many
// bytes our image
// consists of. Use a
// different value than
// 4 if you don't use
// 32bit images.
// ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new
// buffer
// bm.copyPixelsToBuffer(buffer); // Move the byte data to the buffer
// byte[] array = buffer.array(); // Get the underlying array containing
// the data.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] array = stream.toByteArray();
return array;
}
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 140;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 3;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o2);
}
public String getRealPathFromURI(Uri contentUri) {
// can post image
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, // Which columns to
// return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
private Bitmap decodeFile(File f) {
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 10;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
}
return b;
}
希望这会帮助你