我正在开发一个图像编辑Android应用程序。在我的一个活动中,我打算从onCreate中的画廊中选择一个图像:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
然后我收到这样的数据:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Crashlytics.log("onActivityResult called");
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK && data != null) {
Crashlytics.log("Data received from image pick intent");
imageUri = data.getData();
loadImage();
} else {
//if we do not select a picture, go back to the dashboard
Crashlytics.log("Data not received");
onBackPressed();
Log.d(TAG, "no picture selected");
}
}
loadImage
方法:
private void loadImage() {
try {
photoBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (IOException e) {
Crashlytics.log("IOException from getBitmap");
Log.d(TAG, e.getMessage());
showToastAndPressBack();
return;
}
if (photoBitmap == null) {
Crashlytics.log("photoBitmap is null in onActivityResult");
showToastAndPressBack();
return;
}
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
imgVWidth = size.x;
int height = size.y;
imgVHeight = (int) (((float) imgVWidth / photoBitmap.getWidth()) * photoBitmap.getHeight());
photoInImgViewBitmap = Bitmap.createScaledBitmap(photoBitmap, imgVWidth, imgVHeight, true);
imageAlreadyPicked = true;
}
现在我的问题是,有时我看到Crashlytics中的NPE-s声称当用户按下下一个按钮时,photoBitmap为空。
@OnClick(R.id.toolbar_next)
void onToolbarNextClick() {
float originalScale = (float) (previewImageView.getHeight()) / (float) (photoBitmap.getHeight());
...
}
我看到的唯一一个Crashlytics日志是用户离开了意图(我在onPause
内放置了一个Crashlytics日志)。没有onActivityResult
的日志,所以我最好的猜测是onActivityResult
没有被调用,因此我的位图没有加载,因此当用户按下它时它将为null。
问题:为什么有时会调用onActivityResult
,有时不会?是否有photoBitmap
为空的其他可能原因?
答案 0 :(得分:1)
您必须添加以下代码才能从设备中获取图像
"react-native-localize": "^2.0.1",
"react-native": "~0.63.4",
答案 1 :(得分:-1)
要开始相机活动,请使用此代码
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
获取位图
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK){
if(requestCode==CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data"); }
}else{
//Camera request cancelled}
}
答案 2 :(得分:-1)
试试这个工作完美
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, LOAD_IMAGE_RESULTS);
@SuppressLint("Recycle")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = {MediaStore.Images.Media.DATA};
@SuppressLint("Recycle") Cursor cursor = null;
if (pickedImage != null) {
cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
}
if (cursor != null) {
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
}
}
}
现在您可以将imagepath
转换为位图
答案 3 :(得分:-1)
使用此选项从图库中选择:
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivityForResult(i, REQUEST_CODE);
现在你将在onActivityResult中获得一个回调:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE:
if (resultCode != RESULT_CANCELED) {
//use data to get the selected image
}
break;
}
}
答案 4 :(得分:-2)
在代码最后一行方法后调用super.onActivityResult
。当你给超级电话打电话时,你告诉安卓你完成了。
我还注意到你记录了一个char序列。不要这样做你不会用这个解决任何问题。
Crashlytics.log("Data not received");
您需要保存一些数据。认为用户已取消,因为您的代码无法捕获此内容。
/ *假设log方法有一个int输入* /
Crashlytics.log("Data not received");
Crashlytics.log(requestCode);
Crashlytics.log(resultCode);