我需要将图像从相机,图库存储到firebase。在我的项目从画廊工作上传,但当我尝试从相机上传没有任何反应。单击按钮时,您可以选择图库或相机。拍照或从画廊中选择图像时,在imageview中我得到图片。然后我点击保存,如果图片来自图库然后保存到存储,并在数据库中创建子,但如果图片来自相机,它将无法工作。这个问题有解决方案吗?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Uri mImageUri = null;
private Button btn,mSubmitBtn;
private ImageView imageview;
private int GALLERY = 1, CAMERA = 2;
private StorageReference mStorage;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
mSubmitBtn = (Button)findViewById(R.id.submit);
imageview = (ImageView) findViewById(R.id.iv);
mStorage = FirebaseStorage.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Upload");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPictureDialog();
}
});
mSubmitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startPosting();
}
});
}
private void showPictureDialog(){
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems = {
"Select photo from gallery",
"Capture photo from camera" };
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
choosePhotoFromGallary();
break;
case 1:
takePhotoFromCamera();
break;
}
}
});
pictureDialog.show();
}
public void choosePhotoFromGallary() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY);
}
private void takePhotoFromCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
}
////////////////////////////
private void startPosting(){
if (mImageUri !=null){
StorageReference filepath = mStorage.child("Images").child(mImageUri.getLastPathSegment());
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();//push kreira uniq random id
newPost.child("image").setValue(downloadUrl.toString());
}
});
}
}
////////////////////////
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED) {
return;
}
if (requestCode == GALLERY) {
if (data != null) {
mImageUri = data.getData();
imageview.setImageURI(mImageUri);
}
} else if (requestCode == CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageview.setImageBitmap(thumbnail);
imageview.setImageURI(mImageUri);
}
}
}
答案 0 :(得分:0)
我遇到了同样的问题,这主要发生在Kitkat和更低版本上。点击图像比直接获取位图文件尝试按照以下方式进行,
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
imageview.setImageBitmap(thumbnail);
同样在清单文件中,请确保您已经与READ_EXTERNAL
一起声明了以下权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
此问题的原因是因为Android 4.4(KitKat)已更改SD卡上的权限处理方式。因此,最佳做法是在SD卡上创建自己的目录,以处理应用程序需要编写的所有文件。这适用于所有更高版本。
希望这有帮助。