Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:26.1.0'
implementation 'com.squareup.picasso:picasso:2.3.2'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nvmanh.demogallery">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
80 changes: 80 additions & 0 deletions app/src/main/java/com/example/nvmanh/demogallery/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,93 @@
package com.example.nvmanh.demogallery;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_PERMISSION = 1;
private static final String PATH = Environment.getExternalStorageDirectory().getPath().toString();
private static final String PATH_IMAGE = PATH + "/DCIM/Camera";
private static final String PNG = ".png";
private static final String JPG = ".jpg";
private static final String JPEG = ".jpeg";
private static final int LENGTH_CUT = 32;
private RecyclerView mRecyclerGallery;
private List<Picture> mPictures;
private RecyclerAdapterImage mAdapterImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mPictures = new ArrayList<>();
mRecyclerGallery.setLayoutManager(new GridLayoutManager(this, 2));
mAdapterImage = new RecyclerAdapterImage(this, mPictures);
mRecyclerGallery.setAdapter(mAdapterImage);
checkPermission();
}

private void initView() {
mRecyclerGallery = findViewById(R.id.recycler_gallery);
}

private void checkPermission(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION);
}
} else {
getImageFromGallery();
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == REQUEST_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED){
getImageFromGallery();
} else {
finish();
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

private void getImageFromGallery(){
File file = new File(PATH_IMAGE);
File[] images = file.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.getAbsolutePath().endsWith(PNG)
|| file.getAbsolutePath().endsWith(JPG)
|| file.getAbsolutePath().endsWith(JPEG);
}
});

for(File f : images){
Picture p = new Picture(f.getAbsolutePath().substring(LENGTH_CUT), f.getAbsolutePath());
mPictures.add(p);
mAdapterImage.notifyDataSetChanged();
}
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/example/nvmanh/demogallery/Picture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.nvmanh.demogallery;

public class Picture {
private String name;
private String pathImage;

public Picture(String name, String pathImage) {
this.name = name;
this.pathImage = pathImage;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPathImage() {
return pathImage;
}

public void setPathImage(String pathImage) {
this.pathImage = pathImage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.nvmanh.demogallery;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

public class RecyclerAdapterImage extends RecyclerView.Adapter<RecyclerAdapterImage.ViewHolder> {

private List<Picture> mPictures;
private Context mContext;

public RecyclerAdapterImage(Context mContext, List<Picture> mPictures) {
this.mContext = mContext;
this.mPictures = mPictures;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater =
(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_image_recycler, parent, false);

return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Picture p = mPictures.get(position);
holder.text.setText(p.getName());
Picasso.with(mContext).load(new File(p.getPathImage()))
.into(holder.image);
}

@Override
public int getItemCount() {
return mPictures.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView text;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image_demo);
text = itemView.findViewById(R.id.text_name);
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_add_circle_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#1CA5FF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM17,13h-4v4h-2v-4L7,13v-2h4L11,7h2v4h4v2z"/>
</vector>
17 changes: 10 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_black"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
/>

</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
28 changes: 28 additions & 0 deletions app/src/main/res/layout/item_image_recycler.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_200"
android:layout_margin="@dimen/dp_2"
>

<ImageView
android:id="@+id/image_demo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
/>

<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/sp_15"
android:textColor="@color/color_white"
android:layout_margin="@dimen/dp_2"
android:maxLines="1"
android:ellipsize="end"
android:layout_gravity="bottom|center"
/>

</FrameLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="color_white">#ffffff</color>
<color name="color_black">#000</color>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="dp_2">2dp</dimen>
<dimen name="dp_200">200dp</dimen>
<dimen name="sp_15">15sp</dimen>
</resources>