Profile page working, better quality picture aswell. Only needs personal feed.

This commit is contained in:
Niels Zwemmer
2017-06-26 16:13:44 +02:00
parent e54e423847
commit d4d10e3f6f
7 changed files with 81 additions and 159 deletions

View File

@@ -3,8 +3,8 @@
package="nl.myhyvesbookplus.tagram">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18"/>
<application
@@ -14,6 +14,18 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="nl.myhyvesbookplus.tagram.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
<activity
android:name=".MainActivity"
android:label="@string/app_name">

View File

@@ -17,7 +17,7 @@ import com.google.firebase.auth.FirebaseAuth;
import nl.myhyvesbookplus.tagram.controller.DownloadClass;
import nl.myhyvesbookplus.tagram.controller.ProfilePictureUploader;
public class MainActivity extends AppCompatActivity implements CameraFragment.OnFragmentInteractionListener, ProfileFragment.OnFragmentInteractionListener, TimelineFragment.OnFragmentInteractionListener, ProfilePictureUploader.ProfilePictureUpdatedListener {
public class MainActivity extends AppCompatActivity implements CameraFragment.OnFragmentInteractionListener, TimelineFragment.OnFragmentInteractionListener, ProfilePictureUploader.ProfilePictureUpdatedListener {
final static private String TAG = "MainScreen";
FirebaseAuth mAuth;

View File

@@ -1,19 +1,14 @@
package nl.myhyvesbookplus.tagram;
import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -32,88 +27,38 @@ import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import nl.myhyvesbookplus.tagram.controller.UploadClass;
import nl.myhyvesbookplus.tagram.controller.ProfilePictureUploader;
import static android.app.Activity.RESULT_OK;
import static java.lang.System.currentTimeMillis;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link ProfileFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link ProfileFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class ProfileFragment extends Fragment implements View.OnClickListener {
// static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_TAKE_PHOTO = 1;
final static private String TAG = "ProfileFragment";
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private static Uri downloadUrl;
/// Views, buttons and other protected declarations ///
protected Button changePwdButton;
protected ImageButton profilePicButton;
/// Views ///
protected StorageReference httpsReference;
protected TextView profileName;
protected ImageView profilePicture;
protected FirebaseUser user;
protected File photoFile = null;
ProgressDialog progressDialog;
String mCurrentPhotoPath;
File photoFile = null;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
/// Required empty public constructor ///
public ProfileFragment() {}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ProfileFragment.
*/
// TODO: Rename and change types and number of parameters
public static ProfileFragment newInstance(String param1, String param2) {
ProfileFragment fragment = new ProfileFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
user = FirebaseAuth.getInstance().getCurrentUser();
}
/**
* Assigns all views.
* Assigns all views and buttons.
*/
protected void findViews(View view) {
profilePicButton = (ImageButton) view.findViewById(R.id.profile_pic_button);
@@ -123,22 +68,30 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
bindOnClick();
}
/**
* Bind the buttons to their listeners.
*/
protected void bindOnClick() {
profilePicButton.setOnClickListener(this);
changePwdButton.setOnClickListener(this);
}
/// Setup ///
/// Page setup ///
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_profile, container, false);
findViews(view);
if (user != null && user.getPhotoUrl() != null) {
httpsReference = FirebaseStorage.getInstance().getReferenceFromUrl(user.getPhotoUrl().toString());
if (user != null) {
if(user.getPhotoUrl() != null) {
httpsReference = FirebaseStorage.getInstance().getReferenceFromUrl(user.getPhotoUrl().toString());
}
if (user.getDisplayName() != null) {
profileName.setText(user.getDisplayName());
}
}
if (httpsReference != null) {
@@ -147,20 +100,9 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
profilePicture.invalidate();
if (user != null && user.getDisplayName() != null) {
profileName.setText(user.getDisplayName());
}
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
/**
* Called when a view has been clicked.
*
@@ -178,39 +120,24 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
}
}
// TODO Make the function actually do something.
/**
* Performs profile picture change action.
*/
public void profilePicOnClick() {
// Log.d(TAG, "profilePicOnClick: JE KAN NOG GEEN FOTO UPLOADEN");
dispatchTakePictureIntent();
}
/**
* Starts new intent for access to the built-in camera of device.
*/
// private void dispatchTakePictureIntent() {
// Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
// }
// }
private void dispatchTakePictureIntent() {
private void profilePicOnClick() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
/* Ensure that there's a camera activity to handle the intent */
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
/* Create the File where the photo should go */
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(getActivity(), getString(R.string.image_save_error), Toast.LENGTH_LONG);
}
// Continue only if the File was successfully created
if (photoFile != null) {
// Uri uri = Uri.fromFile(photoFile); //TODO Als je dit in takePictureIntent zet ipv photoFile, grote boos.
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile);
Uri photoURI = FileProvider.getUriForFile(getActivity(),
"nl.myhyvesbookplus.tagram.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
@@ -218,35 +145,29 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
/**
* Grabs the image just taken by the built-in camera and pushes this image to the user account.
* @param requestCode
* @param resultCode
* @param data
* @param requestCode The code which corresponds to REQUEST_TAKE_PHOTO. Used as indicator.
* @param resultCode Code should be RESULT_OK to allow camera to proceed.
* @param data The image data from the camera.
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
progressDialog = ProgressDialog.show(getActivity(), getString(R.string.please_wait), "bezig met uploaden", false, false);
// Bitmap imageBitmap = Bitmap.createBitmap();
progressDialog = ProgressDialog.show(getActivity(), getString(R.string.please_wait), getString(R.string.upload_profile_pic), false, false);
ProfilePictureUploader profilePictureUploader = new ProfilePictureUploader(getActivity());
// profilePictureUploader.uploadProfilePicture(imageBitmap);
profilePictureUploader.uploadProfilePicture(photoFile.getAbsoluteFile());
}
}
private File createImageFile() throws IOException {
// Create an image file name
String imageFileName = "JPEG_" + currentTimeMillis();
String imageFileName = "JPEG_" + user.getUid();
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
return File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
@@ -271,43 +192,4 @@ public class ProfileFragment extends Fragment implements View.OnClickListener {
// TODO Add code here for when there is no currently active user.
}
}
/**
* Obligatory onAttach function included in fragments.
* @param context provided context for the function to operate on.
*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
/**
* Obligatory onDetach function included in fragments.
*/
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* See the Android Training lesson http://developer.android.com/training/basics/fragments/communicating.html
* for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}

View File

@@ -5,6 +5,7 @@ import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
@@ -12,9 +13,12 @@ import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.UserProfileChangeRequest;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageMetadata;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.File;
import static java.lang.System.currentTimeMillis;
/**
@@ -39,10 +43,13 @@ public class ProfilePictureUploader extends UploadClass {
}
}
public void uploadProfilePicture(Bitmap picture) {
byte[] uploadPhoto = bitmapToBytes(picture);
public void uploadProfilePicture(File picture) {
oldPicture = mUser.getPhotoUrl();
UploadTask photoUpload = mStorageRef.child("profile").child(getUserUid() + "_" + currentTimeMillis()).putBytes(uploadPhoto);
StorageMetadata metadata = new StorageMetadata.Builder()
.setContentType("image/jpg")
.build();
UploadTask photoUpload = mStorageRef.child("profile").child(getUserUid() + "_" + currentTimeMillis()).putFile(Uri.fromFile(picture), metadata);
photoUpload.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
@@ -80,9 +87,9 @@ public class ProfilePictureUploader extends UploadClass {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "onComplete: Delete successfull");
Log.v(TAG, "onComplete: Delete successful");
} else {
Log.d(TAG, "onComplete: " + task.getException().getLocalizedMessage());
Log.v(TAG, "onComplete: " + task.getException().getLocalizedMessage());
}
}
});

View File

@@ -17,5 +17,17 @@
<string name="profile_picture_button">Wijzig Profiel Foto</string>
<string name="profile_picture_description">profiel foto</string>
<string name="change_psw_button">Wachtwoord wijzigen</string>
<string name="please_wait">Wacht a.u.b.</string>
<string name="please_wait">Momentje</string>
<string name="hello_blank_fragment">Hallo leeg fragment</string>
<string name="hello_camera">Hallo Camera fragment</string>
<string name="login_error">Voer alstublieft email en wachtwoord in</string>
<string name="mail_failed">Er is een fout opgetreden. Controleer internetverbinding.</string>
<string name="mail_successful">Er is een e-mail verzonden. Volg a.u.b. de instructies.</string>
<string name="password_match_error">Wachtwoorden komen niet overeen</string>
<string name="register_error">Vul alstublieft alle velden in</string>
<string name="save">Opslaan</string>
<string name="upload">Uploaden</string>
<string name="image_save_error">Foto opslaan mislukt. Zorg a.u.b. dat er genoeg ruimte op uw telefoon beschikbaar is.</string>
<string name="update_profile_pic_error">Het updaten van de profielfoto is mislukt. Controleer uw internetverbinding.</string>
<string name="upload_profile_pic">Profielfoto aan het uploaden...</string>
</resources>

View File

@@ -19,7 +19,7 @@
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="hello_camera">Hello Camera</string>
<string name="logo_text">MyHyvesBookPlusTagram logo</string>
<string name="logout_button">LogOut</string>
<string name="logout_button">Logout</string>
<string name="profile_picture_button">Change Profile Picture</string>
<string name="profile_picture_description">profile picture</string>
<string name="change_psw_button">Change Password</string>
@@ -29,4 +29,7 @@
<string name="upload">Upload</string>
<string name="save">Save</string>
<string name="image_save_error">Saving image to storage failed. Please make sure there is space available on the device.</string>
<string name="update_profile_pic_error">Updating the profile picture failed. Please check network connection.</string>
<string name="upload_profile_pic">Uploading profile picture...</string>
</resources>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="profile_pic_URI" path="Pictures/" />
</paths>
</resources>