该代码可以正常运行,但是创建了Samsung设备(版本4和5),该问题导致图像需要使用相机并加载imageview,从而无法将图像加载到android应用程序中。
代码:
Bitmap profilebitmap=null;
public void openCameraForProfile(){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 101);//camera open and intent onActivityResult.
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == 101 && resultCode == RESULT_OK) {
try{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
iv_profile.setImageBitmap(thumbnail);
profilebitmap = thumbnail;
}catch (Exception e){
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
app gradle
implementation 'com.fxn769:pix:1.4.4'
place in to your activity for you click button for take picture
private int PICK_IMAGE_REQUEST = 1001;
Options options = Options.init()
.setRequestCode(PICK_IMAGE_REQUEST)
//Request code for activity results
.setCount(3)
//Number of images to restict selection count
.setFrontfacing(false)
//Front Facing camera on start
.setImageQuality(ImageQuality.HIGH)
//Image Quality
.setPreSelectedUrls(returnValue)
//Preselected Image Urls
.setScreenOrientation(Options.SCREEN_ORIENTATION_PORTRAIT)
//Orientaion
.setPath("/pix/images");
//Custom Path For Image Storage
Pix.start(MainActivity.this, options);
receive
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
try {
if (resultCode == RESULT_OK) {
if (requestCode == PICK_IMAGE_REQUEST) {
ArrayList<String> selectedPaths =
data.getStringArrayListExtra(Pix.IMAGE_RESULTS);
if (selectedPaths != null) {
for (int i = 0; i < selectedPaths.size(); i++) {
String fPath = selectedPaths.get(i);
if (!returnValue.contains(fPath)) {
Uri fileUri = Uri.fromFile(new File(fPath));
getImage(fileUri, fPath);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
show image
private void getImage(Uri fileUri, String mFilePath) {
try {
if (fileUri != null) {
Bitmap bitmap =
MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),
fileUri);
bitmap = getResizedBitmap(bitmap, 1080);
} catch (IOException e) {
e.printStackTrace();
}
}
resize the image
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
if (width > maxSize || height > maxSize) {
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
} else {
return image;
}
}