我有这个代码从图库上传图像或用户可以使用他们的相机拍照,但相机的结果代码返回-1并崩溃,老实说我不知道为什么。这是代码:
private Button mSelectImage;
private Button upload;
private ImageView mImage;
private StorageReference mStorage;
private static final int GALLERY_INTENT = 2;
private static final int CAMERA_REQUEST_CODE = 7;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStorage = FirebaseStorage.getInstance().getReference();
mSelectImage = (Button) findViewById(R.id.selectImage);
upload = (Button) findViewById(R.id.captureUpload);
progressDialog = new ProgressDialog(this);
mImage = (ImageView) findViewById(R.id.image);
mSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivityForResult(i, GALLERY_INTENT);
}
});
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK)
{
progressDialog.setMessage("uploading...");
progressDialog.show();
Uri uri = data.getData();
StorageReference filePath =
mStorage.child("Photos").child(uri.getLastPathSegment());
filePath.putFile(uri).addOnSuccessListener(new
OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
Toast.makeText(MainActivity.this, "Image upload
successful",Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
else if(requestCode == CAMERA_REQUEST_CODE && resultCode ==
RESULT_OK)
{
progressDialog.setMessage("uploading...");
progressDialog.show();
Uri uri = data.getData();
StorageReference filePath =
mStorage.child("Photos").child(uri.getLastPathSegment());
filePath.putFile(uri).addOnSuccessListener(new
OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
Toast.makeText(MainActivity.this, "Image upload
successful",Toast.LENGTH_LONG).show();
progressDialog.dismiss();
Uri downloadUri = taskSnapshot.getDownloadUrl();
Picasso.with(MainActivity.this)
.load(downloadUri).fit().centerCrop().into(mImage);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
}
}
当我从图库上传但在我拿相机后在相机上返回-1请求结果时,它可以正常工作。它甚至没有进入下载部分(使用Picasso)
非常感谢你的帮助!!!! 最好的问候
java.lang.RuntimeException:将结果ResultInfo {who = null,request = 7,result = -1,data = Intent {act = inline-data(has extras)}}传递给activity {com.example.user .uploadingimages / com.example.user.uploadingimages.MainActivity}:java.lang.IllegalArgumentException:uri不能为null
答案 0 :(得分:0)
如果没有logcat,我会说这可能是一个权限问题:
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
如果发布logcat我可以更精确
编辑:
logcat清楚地说明了问题
java.lang.IllegalArgumentException: uri cannot be null
您是否正确检查此语句是否返回非空值?
Uri uri = data.getData();
答案 1 :(得分:0)
根据日志,data.getData()返回null。
在ACTION_IMAGE_CAPTURE Intent中提供EXTRA_OUTPUT,在这种情况下,您知道应该存储图像的位置,而不需要依赖getData()
点击相机按钮,请使用以下代码:
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment
.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(f));
startActivityForResult(intent,
CAMERA_REQUEST_CODE);
参考:Android data.getData() returns null from CameraActivity for some phones