我想将图片上传到我的数据库并订购。我的上传代码如下所示 -
package com.example.cagri.cafeorderapp;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.IOException;
public class AddFood extends AppCompatActivity {
private int PICK_IMAGE_REQUEST = 1;
private ImageButton foodImage;
private static final int GELLERQ=1;
private EditText name, desc, price;
private Uri uri=null;
private StorageReference storageReference = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_food);
name=(EditText)findViewById(R.id.itemName);
desc=(EditText)findViewById(R.id.itemDesc);
price=(EditText)findViewById(R.id.itemPrice);
storageReference= FirebaseStorage.getInstance().getReference("Item");
}
public void imageButtonClicked(View view)
{
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
PICK_IMAGE_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
ImageView imageView = (ImageView) findViewById(R.id.foodImageButton);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void addItemButtonClicked(View view)
{
String name_text=name.getText().toString().trim();
String desc_text=desc.getText().toString().trim();
String price_text=price.getText().toString().trim();
if(!TextUtils.isEmpty(name_text) && !TextUtils.isEmpty(desc_text) && !TextUtils.isEmpty(price_text))
{
StorageReference filepath=storageReference.child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
final Uri downloadurl = taskSnapshot.getDownloadUrl();
Toast.makeText(AddFood.this,"Image uploaded",Toast.LENGTH_LONG).show();
}
});
}
}
但我得到的错误是
尝试调用虚方法'java.lang.String 关于空对象引用的android.net.Uri.getLastPathSegment()'
一切都很好,我可以将项目图像放在imagebutton
中,但是当我点击添加项目时,该过程就会停止。
答案 0 :(得分:0)
Uri uri = data.getData();
(和其他东西)不幸的是,选择图片不是一个好方法
您需要使用带有文件选择的光标&#34;传统方式&#34; 获取真实路径
查看我的例子
此文件包含调用cursorloader
的示例此文件包含所需的util方法
或强>
如果您处理rxjava,我会建议使用此库https://github.com/MLSDev/RxImagePicker
它会给你一个正确的uri