Image Fragment

 first Create java class named ImageModel :

ImageModel.java

 public class ImageModel {

    private String name,path,fileName;
private Uri uri;

public ImageModel() {
}

public ImageModel(String name, String path, String fileName, Uri uri) {
this.name = name;
this.path = path;
this.fileName = fileName;
this.uri = uri;
}

public String getName() {
return name;
}

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

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public Uri getUri() {
return uri;
}

public void setUri(Uri uri) {
this.uri = uri;
}
}

Create a java class named "Constant"

public class Constant {
public static final String FOLDER_NAME ="/WhatsApp/";
public static final String SAVE_FOLDER_NAME ="/WS Downloader/";
}

create a layout resources file named "item.xml " 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:elevation="2dp"
android:layout_marginBottom="5dp"
>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="350dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<ImageView
android:id="@+id/vView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/ic_baseline_image_24" />
</LinearLayout>

<ImageView
android:id="@+id/vPlay"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/play"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/linearLayout" />

</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="4dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/vShare"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_baseline_share_24"
app:tint="@color/teal_700" />
<ImageView
android:id="@+id/vLine"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:rotation="90"
android:scaleType="centerCrop"
android:src="@drawable/line"
app:tint="#D7D8D8" />


<ImageView
android:id="@+id/vDownload"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_baseline_cloud_download_24"
app:tint="@color/teal_700" />

</LinearLayout>

</LinearLayout>


Create a Java Class  named "ImageAdapter"

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {

private Context context;
private ArrayList<Object> imageList;

public ImageAdapter(Context context, ArrayList<Object> imageList) {
this.context = context;
this.imageList = imageList;
}

@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ImageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false));
}

@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {

checkFolder();
final ImageModel file=(ImageModel)imageList.get(position);
if (!file.getUri().toString().endsWith(".mp4")){
holder.vPlay.setVisibility(View.GONE);
Glide.with(context)
.load(file.getUri().toString())
.centerCrop()
.placeholder(R.drawable.ic_baseline_image_24)
.into(holder.vView);
}
holder.vDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

checkFolder();

final String path =((ImageModel)imageList.get(position)).getPath();
final File file=new File(path);

String destPath = Environment.getExternalStorageDirectory().getAbsolutePath() + Constant.SAVE_FOLDER_NAME;
File destFile= new File(destPath);

try {
FileUtils.copyFileToDirectory(file,destFile);
} catch (IOException e) {
e.printStackTrace();
}


MediaScannerConnection.scanFile(
context,
new String[]{destPath + ((ImageModel) imageList.get(position)).getFileName()},
new String[]{"*/*"},
new MediaScannerConnection.MediaScannerConnectionClient() {
@Override
public void onMediaScannerConnected() {

}

@Override
public void onScanCompleted(String s, Uri uri) {

}
}
);

Toast.makeText(context,"Saved to "+destPath+((ImageModel) imageList.get(position)).getFileName(),Toast.LENGTH_SHORT).show();

}
});

holder.vShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File localFile=new File(file.getPath().toString());

Uri imageUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName()+".fileprovider",localFile);
Intent shareIntent= ShareCompat.IntentBuilder.from((Activity) context)
.setStream(imageUri)
.setType("image/jpg")
.setChooserTitle("WS Downloader")
.getIntent();

if (shareIntent.resolveActivity(context.getPackageManager())!= null){
context.startActivity(shareIntent);
}
}
});

}

private void checkFolder() {
String path= Environment.getExternalStorageDirectory().getAbsolutePath() + Constant.SAVE_FOLDER_NAME;
File dir= new File(path);

boolean isDirectoryCreated =dir.exists();

if (!isDirectoryCreated){
isDirectoryCreated=dir.mkdir();
}
if (isDirectoryCreated) {
}
}

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

public class ImageViewHolder extends RecyclerView.ViewHolder{

ImageView vView,vShare,vDownload,vPlay;

public ImageViewHolder(@NonNull View itemView) {
super(itemView);
vView=itemView.findViewById(R.id.vView);
vShare=itemView.findViewById(R.id.vShare);
vDownload=itemView.findViewById(R.id.vDownload);
vPlay=itemView.findViewById(R.id.vPlay);

}
}


}

ImageFragment Code:

fragment_image.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/imageSwipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ImageFragment">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/imageRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F1EBEB"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

ImageFragment.java
public class ImageFragment extends Fragment {


private SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView recyclerView;
private ImageAdapter imageAdapter;
private File[] files;
private ArrayList<Object> filesList= new ArrayList<>();

public ImageFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_image, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init(view);

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
setUpRefrashLayout();
(
new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
}
},2000);

}
});

Dexter.withContext(getContext()).withPermissions(Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport multiplePermissionsReport) {
setUpRefrashLayout();
}

@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> list, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check();

}

private void setUpRefrashLayout() {
filesList.clear();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
imageAdapter= new ImageAdapter(getContext(),getData());
recyclerView.setAdapter(imageAdapter);
imageAdapter.notifyDataSetChanged();
}

private ArrayList<Object> getData() {
ImageModel f;
String targetPath = Environment.getExternalStorageDirectory().getAbsolutePath()+Constant.FOLDER_NAME+"Media/.Statuses";
File targetDirectory =new File(targetPath);
files = targetDirectory.listFiles();

for (int i=0;i<files.length;i++){

File file= files[i];
f=new ImageModel();
f.setUri(Uri.fromFile(file));
f.setPath(files[i].getAbsolutePath());
f.setFileName(file.getName());

if (f.getUri().toString().endsWith(".jpg")){
filesList.add(f);
}
}
return filesList;
}

private void init(View view) {
swipeRefreshLayout=view.findViewById(R.id.imageSwipe);
recyclerView=view.findViewById(R.id.imageRecyclerView);

}
}



Post a Comment

0Comments
Post a Comment (0)