Firebase的图像旋转了-90度

时间:2017-03-05 21:34:12

标签: android firebase firebase-realtime-database firebase-storage

当我在Firebase上传图像时,肖像图像会旋转-90度。因此,Firebase上的所有图像都是横向的。我怎么知道,当我加载图像时,我必须将哪个图像旋转到90度才能以正常方向查看而哪些图像不能?

public class DialogAdd extends DialogFragment {

EditText ime;
EditText opis;
EditText cena;
EditText kategorija;
Button slikaj;
Button dodaj;
ArrayList<Uri> slike=new ArrayList<>();
ArrayList<String> slike2=new ArrayList<>();

ProgressDialog progressDialog;

ArrayList<Item> items = new ArrayList<>();

UploadTask uploadTask;
String pic;

FirebaseStorage storage;
StorageReference storageRef;
StorageReference mountainImagesRef;

DatabaseReference myRef;

private Task<Void> allTask;

private FirebaseAuth mFirebaseAuth;
private FirebaseUser mFirebaseUser;
private DatabaseReference mDatabase;

private View.OnClickListener mBtnAddListener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i,1);
    }
};
private View.OnClickListener mBtnAddListener2 = new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        try {
            for (int i = 0; i < slike.size(); i++) {

                Bitmap bitmapImage = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), slike.get(i));
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmapImage.compress(Bitmap.CompressFormat.JPEG, 50, baos);
                byte[] dataBytes = baos.toByteArray();

                pic = "Image" + i + ".jpg";

                mountainImagesRef = storageRef.child(usernameFromEmail(mFirebaseUser.getEmail())).child(ime.getText().toString()).child(pic);

                uploadTask = mountainImagesRef.putBytes(dataBytes);

                uploadTask.addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {


                        // Handle unsuccessful uploads
                        Toast.makeText(getContext(), "There was an error - please try again!", Toast.LENGTH_SHORT).show();
                    }
                }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                        Uri downloadUrl = taskSnapshot.getDownloadUrl();



                        Toast.makeText(getContext(),pic +" uploaded", Toast.LENGTH_SHORT).show();
                        slike2.add(downloadUrl.toString());

                        Log.i("TEST",slike2.toString());


                    }
                } );
            }

            allTask = Tasks.whenAll(uploadTask);
            allTask.addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {

                    //DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("trails").child(usernameFromEmail(mFirebaseUser.getEmail())).child(trail.getUnique_id()).push();
                    myRef = FirebaseDatabase.getInstance().getReference().child("Items").push();

                    Item item = new Item(usernameFromEmail(mFirebaseUser.getEmail()),ime.getText().toString(),opis.getText().toString(),Integer.parseInt(cena.getText().toString()),kategorija.getText().toString(),usernameFromEmail(mFirebaseUser.getEmail())+"_"+kategorija.getText().toString(),slike2);
                    item.setProfilePic(slike2.get(0));
                    items.add(item);
                    myRef.setValue(item);




                    Toast.makeText(getContext(), "Your item has been posted!", Toast.LENGTH_SHORT).show();
                    dismiss();

                }
            });
            allTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    // apologize profusely to the user!
                }
            });






        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
};

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getDialog().setCanceledOnTouchOutside(true);

    View view = inflater.inflate(R.layout.activity_uploader,
            container, false);
    //TODO:findViewById, etc
    return view;
}
public void onResume()
{
    super.onResume();
    Window window = getDialog().getWindow();
    window.setLayout(700,900);
    window.setGravity(Gravity.CENTER);
    //TODO:
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseUser = mFirebaseAuth.getCurrentUser();
    mDatabase = FirebaseDatabase.getInstance().getReference();

    storage = FirebaseStorage.getInstance();
    storageRef = storage.getReferenceFromUrl("gs://materialdesign-fb467.appspot.com");

    ime= (EditText) view.findViewById(R.id.ime);
    opis= (EditText) view.findViewById(R.id.opis);
    cena= (EditText) view.findViewById(R.id.cena);
    slikaj= (Button) view.findViewById(R.id.slikaj);
    dodaj= (Button) view.findViewById(R.id.dodaj);
    kategorija= (EditText) view.findViewById(R.id.editKategorija);


    slikaj.setOnClickListener(mBtnAddListener);
    dodaj.setOnClickListener(mBtnAddListener2);


}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode==1 && resultCode==RESULT_OK && data!=null) {
        Uri selectedImage = data.getData();
        slike.add(selectedImage);

    }
}






private String usernameFromEmail(String email) {

    if (email.contains("@")) {
        return email.split("@")[0];
    } else {
        return email;
    }
}
}

0 个答案:

没有答案