diff --git a/app/build.gradle b/app/build.gradle index 82cde8e..cf9c8cd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,7 +26,6 @@ android { dependencies { implementation "com.android.support:appcompat-v7:27.1.1" implementation "com.android.support:design:27.1.1" - implementation "com.android.support:support-v13:27.1.1" implementation project(':libary') implementation project(':rxjava') implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fd11350..4f168c0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,8 @@ - + package="net.alhazmy13.mediapickerexample"> + + - + android:theme="@style/AppTheme"> + diff --git a/app/src/main/java/net/alhazmy13/mediapickerexample/ImageFragment.java b/app/src/main/java/net/alhazmy13/mediapickerexample/ImageFragment.java index 9ed0032..205e3ba 100644 --- a/app/src/main/java/net/alhazmy13/mediapickerexample/ImageFragment.java +++ b/app/src/main/java/net/alhazmy13/mediapickerexample/ImageFragment.java @@ -1,8 +1,9 @@ package net.alhazmy13.mediapickerexample; -import android.app.Fragment; +import android.content.Intent; import android.graphics.BitmapFactory; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -11,13 +12,12 @@ import android.widget.TextView; import net.alhazmy13.mediapicker.Image.ImagePicker; -import net.alhazmy13.mediapicker.rxjava.image.ImagePickerHelper; import java.util.List; import io.reactivex.Observable; -import io.reactivex.Observer; -import io.reactivex.disposables.Disposable; + +import static android.app.Activity.RESULT_OK; /** @@ -38,8 +38,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, View view = inflater.inflate(R.layout.image_layout, container, false); // Find our View instances - imageView = (ImageView) view.findViewById(R.id.iv_image); - path = (TextView) view.findViewById(R.id.tv_path); + imageView = view.findViewById(R.id.iv_image); + path = view.findViewById(R.id.tv_path); view.findViewById(R.id.bt_pick).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -51,50 +51,36 @@ public void onClick(View v) { private void pickImage() { - Observable> x = new ImagePickerHelper( - new ImagePicker.Builder(getActivity()) - .mode(ImagePicker.Mode.CAMERA_AND_GALLERY) - .allowMultipleImages(true) - .compressLevel(ImagePicker.ComperesLevel.MEDIUM) - .directory(ImagePicker.Directory.DEFAULT) - .extension(ImagePicker.Extension.PNG) - .allowOnlineImages(false) - .scale(600, 600) - .allowMultipleImages(true) - .enableDebuggingMode(true)) - .getObservable(); - - x.subscribe(new Observer>() { - @Override - public void onSubscribe(Disposable d) { - - } - - @Override - public void onNext(List imagePath) { - Log.d(TAG, "onNext() called with: " + "imagePath = [" + imagePath + "]"); - mPath = imagePath; - loadImage(); - } - - @Override - public void onError(Throwable e) { - Log.d(TAG, "onError() called with: " + "e = [" + e + "]"); - e.printStackTrace(); - } - - @Override - public void onComplete() { - - } - }); + new ImagePicker.Builder(getActivity()) + .mode(ImagePicker.Mode.CAMERA_AND_GALLERY) + .allowMultipleImages(true) + .compressLevel(ImagePicker.ComperesLevel.MEDIUM) + .directory(ImagePicker.Directory.DEFAULT) + .extension(ImagePicker.Extension.PNG) + .allowOnlineImages(false) + .scale(600, 600) + .allowMultipleImages(true) + .enableDebuggingMode(true) + .build(); + } + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]"); + if (requestCode == ImagePicker.IMAGE_PICKER_REQUEST_CODE && resultCode == RESULT_OK) { + mPath = data.getStringArrayListExtra(ImagePicker.EXTRA_IMAGE_PATH); + Log.d(TAG, "onActivityResult: "); + loadImage(); + } } private void loadImage() { Log.d(TAG, "loadImage: " + mPath.size()); - path.setText(mPath.get(0)); - imageView.setImageBitmap(BitmapFactory.decodeFile(mPath.get(0))); + if (mPath != null && mPath.size() > 0) { + path.setText(mPath.get(0)); + imageView.setImageBitmap(BitmapFactory.decodeFile(mPath.get(0))); + } } } diff --git a/app/src/main/java/net/alhazmy13/mediapickerexample/MainActivity.java b/app/src/main/java/net/alhazmy13/mediapickerexample/MainActivity.java index 3ff31e6..e637eea 100644 --- a/app/src/main/java/net/alhazmy13/mediapickerexample/MainActivity.java +++ b/app/src/main/java/net/alhazmy13/mediapickerexample/MainActivity.java @@ -1,44 +1,42 @@ package net.alhazmy13.mediapickerexample; -import android.app.Fragment; -import android.app.FragmentManager; import android.content.Intent; import android.os.Bundle; import android.support.annotation.VisibleForTesting; import android.support.design.widget.TabLayout; -import android.support.v13.app.FragmentPagerAdapter; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; public class MainActivity extends AppCompatActivity { - private Fragment videoFragment; - private Fragment imageFragment; + private Fragment videoFragment = new VideoFragment(); + private Fragment imageFragment = new ImageFragment(); + private ViewPager mPager; + private PagerAdapter mPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - PickerAdapter adapter = new PickerAdapter(getFragmentManager()); - ViewPager viewPager = (ViewPager) findViewById(R.id.pager); - viewPager.setAdapter(adapter); - - setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); - TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); - tabLayout.setupWithViewPager(viewPager); - for (int i = 0; i < adapter.getCount(); i++) - tabLayout.getTabAt(i).setText(adapter.getTitle(i)); + mPager = findViewById(R.id.pager); + mPagerAdapter = new PickerAdapter(getSupportFragmentManager()); + mPager.setAdapter(mPagerAdapter); + TabLayout tabLayout = findViewById(R.id.tabs); + tabLayout.setupWithViewPager(mPager); + for (int i = 0; i < mPagerAdapter.getCount(); i++) + tabLayout.getTabAt(i).setText(mPagerAdapter.getPageTitle(i)); } - private class PickerAdapter extends FragmentPagerAdapter { + private class PickerAdapter extends FragmentStatePagerAdapter { private static final int NUM_PAGES = 2; PickerAdapter(FragmentManager fm) { super(fm); - videoFragment = new VideoFragment(); - imageFragment = new ImageFragment(); } @Override @@ -58,12 +56,12 @@ public Fragment getItem(int position) { } @VisibleForTesting - int getTitle(int position) { + public String getPageTitle(int position) { switch (position) { case 0: - return R.string.tab_title_video; + return getString(R.string.tab_title_video); default: - return R.string.tab_title_image; + return getString(R.string.tab_title_image); } } } diff --git a/app/src/main/java/net/alhazmy13/mediapickerexample/VideoFragment.java b/app/src/main/java/net/alhazmy13/mediapickerexample/VideoFragment.java index f3fe546..32005b4 100644 --- a/app/src/main/java/net/alhazmy13/mediapickerexample/VideoFragment.java +++ b/app/src/main/java/net/alhazmy13/mediapickerexample/VideoFragment.java @@ -1,9 +1,10 @@ package net.alhazmy13.mediapickerexample; -import android.app.Fragment; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -31,7 +32,7 @@ public class VideoFragment extends Fragment { @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.video_layout, container, false); init(view); @@ -41,12 +42,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, private void init(View view) { // Find our View instances - videoView = (VideoView) view.findViewById(R.id.iv_video); + videoView = view.findViewById(R.id.iv_video); MediaController mediaController = new MediaController(getActivity()); mediaController.setAnchorView(videoView); videoView.setMediaController(mediaController); - videoView.start(); - path = (TextView) view.findViewById(R.id.tv_path); + path = view.findViewById(R.id.tv_path); view.findViewById(R.id.bt_pick).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -71,14 +71,12 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]"); if (requestCode == VideoPicker.VIDEO_PICKER_REQUEST_CODE && resultCode == RESULT_OK) { - mPath = (List) data.getSerializableExtra(VideoPicker.EXTRA_VIDEO_PATH); - Log.d(TAG, "onActivityResult: "); + mPath = data.getStringArrayListExtra(VideoPicker.EXTRA_VIDEO_PATH); loadVideo(); } } private void loadVideo() { - Log.d(TAG, "loadVideo: "+ (mPath == null)); if (mPath != null && mPath.size() > 0) { path.setText(mPath.get(0)); videoView.setVideoURI(Uri.parse(mPath.get(0))); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5cfe331..a1d0a1c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,31 +1,19 @@ - + android:layout_height="match_parent" + android:orientation="vertical"> - - - - - - - + android:layout_height="wrap_content" + android:background="@color/colorPrimary" /> + android:layout_height="match_parent" + app:layout_behavior="@string/appbar_scrolling_view_behavior" /> - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/layout/image_layout.xml b/app/src/main/res/layout/image_layout.xml index 8e63d13..78a2848 100644 --- a/app/src/main/res/layout/image_layout.xml +++ b/app/src/main/res/layout/image_layout.xml @@ -1,5 +1,5 @@ - @@ -13,16 +13,18 @@ + android:layout_height="400dp" /> +