Android ImageView 无法从图库或相机获取图像
Android ImageView can't get image from gallery or camera
我想从 Android 上的画廊或相机中挑选一张照片并在 ImageView 上显示照片,打开画廊或相机没问题,权限也没有问题。但是问题是,我select或者我用相机拍的图片在ImageView上没有显示,也没有报错。
下面是我的代码
private static final int CAMERA_REQUEST_CODE = 1;
private static final int SELECT_FILE = 0;
public PersonalDataStep(){
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.personal_data_step, container, false);
ButterKnife.bind(this, view);
//imageEktp.setClickable(true);
getPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Log.d("test : ", "testing");
CharSequence menu[] = new CharSequence[]{"Take From Galery", "Open Camera"};
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Pick a Picture");
builder.setItems(menu, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if(i == 0){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, SELECT_FILE);
}else if(i == 1){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
}
});
builder.show();
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode== Activity.RESULT_OK) {
if (resultCode == CAMERA_REQUEST_CODE) {
Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) bundle.get("data");
imageEktp.setImageBitmap(bitmap);
} else if (resultCode == SELECT_FILE) {
Uri selectedImage = data.getData();
imageEktp.setImageURI(selectedImage);
Log.d("image from galery : ", selectedImage.toString());
}
}else{
Toast.makeText(getActivity(), "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
试试,稍微改变一下你的方法,对我来说效果很好。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode== Activity.RESULT_OK) {
if (requestCode == CAMERA_REQUEST_CODE && data! = null) {
Uri mImageCaptureUri = data.getData();
imageEktp.setImageURI(mImageCaptureUri);
} else if (requestCode == SELECT_FILE && data != null) {
Uri selectedImage = data.getData();
imageEktp.setImageURI(selectedImage);
Log.d("image from galery : ", selectedImage.toString());
}
}else{
Toast.makeText(getActivity(), "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
试试这个代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm=MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(),
data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
ivImage.setImageBitmap(bm);
}
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ivImage.setImageBitmap(thumbnail);
}
我觉得对你有帮助,请检查一下你的代码。
您的代码中有小错字
if (resultCode == CAMERA_REQUEST_CODE) {
...
} else if (resultCode == SELECT_FILE) {
...
}
应该是
if (requestCode == CAMERA_REQUEST_CODE) {
...
} else if (requestCode == SELECT_FILE) {
...
}
我想从 Android 上的画廊或相机中挑选一张照片并在 ImageView 上显示照片,打开画廊或相机没问题,权限也没有问题。但是问题是,我select或者我用相机拍的图片在ImageView上没有显示,也没有报错。
下面是我的代码
private static final int CAMERA_REQUEST_CODE = 1;
private static final int SELECT_FILE = 0;
public PersonalDataStep(){
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.personal_data_step, container, false);
ButterKnife.bind(this, view);
//imageEktp.setClickable(true);
getPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Log.d("test : ", "testing");
CharSequence menu[] = new CharSequence[]{"Take From Galery", "Open Camera"};
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Pick a Picture");
builder.setItems(menu, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if(i == 0){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, SELECT_FILE);
}else if(i == 1){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
}
});
builder.show();
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode== Activity.RESULT_OK) {
if (resultCode == CAMERA_REQUEST_CODE) {
Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) bundle.get("data");
imageEktp.setImageBitmap(bitmap);
} else if (resultCode == SELECT_FILE) {
Uri selectedImage = data.getData();
imageEktp.setImageURI(selectedImage);
Log.d("image from galery : ", selectedImage.toString());
}
}else{
Toast.makeText(getActivity(), "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
试试,稍微改变一下你的方法,对我来说效果很好。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode== Activity.RESULT_OK) {
if (requestCode == CAMERA_REQUEST_CODE && data! = null) {
Uri mImageCaptureUri = data.getData();
imageEktp.setImageURI(mImageCaptureUri);
} else if (requestCode == SELECT_FILE && data != null) {
Uri selectedImage = data.getData();
imageEktp.setImageURI(selectedImage);
Log.d("image from galery : ", selectedImage.toString());
}
}else{
Toast.makeText(getActivity(), "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
试试这个代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm=MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(),
data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
ivImage.setImageBitmap(bm);
}
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ivImage.setImageBitmap(thumbnail);
}
我觉得对你有帮助,请检查一下你的代码。
您的代码中有小错字
if (resultCode == CAMERA_REQUEST_CODE) {
...
} else if (resultCode == SELECT_FILE) {
...
}
应该是
if (requestCode == CAMERA_REQUEST_CODE) {
...
} else if (requestCode == SELECT_FILE) {
...
}