我尝试捕获图片并将其上传到Firebase
。
问题是我在data.getData()
中从onActivityResult
获取了空。
我看过网络,在这种情况下无法找到对我有用的任何内容。
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
这是onActivityResult
:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri uri = data.getData();
StorageReference filePath = storageRef.child("labelPhoto").child(uri.getLastPathSegment())
filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
....rest of the code... }
我无法发送给uri,因为data.geData()
我尝试了许多解决方案,例如将图像保存为bmp并获取此uri:
Bitmap bitmap= (Bitmap)data.getExtras().get("data");
captureImage.setImageBitmap(bitmap);
String mPath = Environment.getExternalStorageDirectory().toString() + "/Pictures/" + picName + ".jpg";
File imageFile = new File(mPath);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
String temp = Environment.getExternalStorageDirectory().getPath() + "/Pictures/";
uri= Uri.fromFile(imageFile);
Log.d("TAG", uri.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
java.net.URI javaUri = java.net.URI.create(uri.toString());
android.net.Uri androidUri = android.net.Uri.parse(uri.toString());
StorageReference filePath = storageRef.child("labelPhoto").child(androidUri.toString());
filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
....rest of the code...}
添加以下支票:
if(resultCode != RESULT_CANCELED){
和
if(data!=null)
清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
没有任何成功。 有人有想法吗?
答案 0 :(得分:1)
如果您正在使用标准的Android相机应用,则应将您希望照片存储的URI传递为额外的意图,如下所示:
Uri outputFileUri = Uri.fromFile(YOUR_FILE);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent(MediaStore.EXTRA_OUTPUT, outputFileUri);