upload picture implemented

still need to link it with create a post and DataBase
This commit is contained in:
Ulysse Widmer 2020-03-18 01:09:39 +09:00
parent 7b5049bba9
commit 2a1b9209e1
5 changed files with 91 additions and 1 deletions

View File

@ -3,6 +3,7 @@
package="com.h.pixeldroid">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"

View File

@ -16,6 +16,7 @@ import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.navigation.NavigationView
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.h.pixeldroid.fragments.CameraFragment
import com.h.pixeldroid.fragments.HomeFragment
import com.h.pixeldroid.fragments.MyProfileFragment
@ -45,7 +46,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
navigationView.setNavigationItemSelectedListener(this)
val tabs =
arrayOf(HomeFragment(), Fragment(), Fragment(), Fragment(), MyProfileFragment())
arrayOf(HomeFragment(), Fragment(), CameraFragment(), Fragment(), MyProfileFragment())
setupTabs(tabs)
}

View File

@ -0,0 +1,59 @@
package com.h.pixeldroid.fragments
import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import com.h.pixeldroid.R
const val PICK_IMAGE_REQUEST = 1
const val TAG = "Camera Fragment"
class CameraFragment : Fragment() {
private var uploadedPictureView: ImageView? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_camera, container, false)
val uploadPictureButton: Button = view.findViewById(R.id.uploadPictureButton)
uploadedPictureView = view.findViewById(R.id.uploadedPictureView)
uploadPictureButton.setOnClickListener{
uploadPicture()
}
// Inflate the layout for this fragment
return view
}
private fun uploadPicture() {
Intent().apply {
type = "image/*"
action = Intent.ACTION_GET_CONTENT
startActivityForResult(
Intent.createChooser(this, "Select a Picture"), PICK_IMAGE_REQUEST
)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
if(data == null || data.data == null){
Log.w(TAG, "No picture uploaded")
return
}
uploadedPictureView?.setImageURI(data.data)
}
}
}

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
tools:context=".fragments.CameraFragment"
android:orientation="vertical">
<Button
android:id="@+id/uploadPictureButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:layout_margin="15dp"
android:text="@string/upload_a_picture"
android:background="@color/colorPrimary"
android:textColor="@color/cardview_light_background"/>
<ImageView
android:id="@+id/uploadedPictureView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>

View File

@ -58,5 +58,6 @@
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="start_login">Start Login</string>
<string name="no_username">No Username</string>
<string name="upload_a_picture">Upload a picture</string>
</resources>