图片显示:
RequestOptions options = new RequestOptions().centerCrop()
.placeholder(R.drawable.abc_vector_test)
.error(R.drawable.ic_launcher_background)
.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.NONE);
//用glide或者picasso都可以,用glide的时候需要申请requestopteins
Glide.with(mContext).load("https://alifei04.cfp.cn/creative/vcg/veer/1600water/veer-303764513.jpg")
.apply(options)
.into(imageView);
Picasso.with(mContext).load("https://alifei04.cfp.cn/creative/vcg/veer/1600water/veer-303764513.jpg")
.resize(300, 300)
.into(imageView);
目录:
MainActivity
package com.example.showpicture;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.showpicture.showLocalPicture.ShowLocalPictureFragment;
import com.example.showpicture.showNetWorkPicture.ShowNetWorkPictureFragment;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
private Button btnLocal;
private Button btnNet;
private FragmentManager fragmentManager;
private FragmentTransaction transaction;
private Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initFragment();
}
private void initView() {
btnLocal = findViewById(R.id.btn_local);
btnNet = findViewById(R.id.btn_net);
btnLocal.setOnClickListener(this::onClick);
btnNet.setOnClickListener(this::onClick);
}
private void initFragment() {
fragmentManager = getSupportFragmentManager();
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_local:
Log.d(TAG, "onClick() btn_local"+fragment);
if (fragment!=null){
transaction.remove(fragment);
}
transaction = fragmentManager.beginTransaction();
fragment = new ShowLocalPictureFragment();
transaction.add(R.id.fragment,fragment);
transaction.commit();
break;
case R.id.btn_net:
Log.d(TAG, "onClick() btn_net"+fragment);
if (fragment!=null){
transaction.remove(fragment);
}
transaction = fragmentManager.beginTransaction();
fragment = new ShowNetWorkPictureFragment();
transaction.add(R.id.fragment,fragment);
transaction.commit();
break;
}
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity"
android:id="@+id/main_activity"
android:orientation="vertical">
<Button
android:id="@+id/btn_local"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100px"
android:text="local"
/>
<Button
android:id="@+id/btn_net"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="100px"
android:text="net"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment"
android:layout_below="@+id/btn_local">
</FrameLayout>
</RelativeLayout>
AndroidManifest.xml权限
<!--在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 从SDCard读取数据权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
build.gradle:
显示网络图片需要加载的jar包,picasso或者glide都可以
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
一、本地图片+recyclerView
fragment:
package com.example.showpicture.showLocalPicture;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.showpicture.R;
import com.example.showpicture.showLocalPicture.adapter.RecyclerViewAdapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
public class ShowLocalPictureFragment extends Fragment {
private static final String TAG = ShowLocalPictureFragment.class.getSimpleName();
private Context mContext;
private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
private ImageView imageView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_local,container,false);
}
@Override
public void onStart() {
super.onStart();
mContext = getContext();
checkNeedPermissions();
initView();
}
private void initView() {
List<String> imgList = new ArrayList<>();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
recyclerView = getView().findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setLayoutManager(new GridLayoutManager(mContext,4));
File file = new File(Environment.getExternalStorageDirectory()+"/textPic");
Log.d(TAG, "initView() file.listFiles();"+ file.listFiles().length);
File[] picFiles = file.listFiles();
for (int i = 0; i<picFiles.length;i++){
imgList.add(picFiles[i].getPath());
}
adapter = new RecyclerViewAdapter(mContext,imgList);
recyclerView.setAdapter(adapter);
recyclerView.setNestedScrollingEnabled(false);
//
}
private void checkNeedPermissions(){
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//多个权限一起申请
ActivityCompat.requestPermissions(getActivity(), new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
}, 1);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_height="1000px"
android:layout_width="match_parent"
android:background="@color/teal_200">
</androidx.recyclerview.widget.RecyclerView>
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="500px"
android:background="@color/design_default_color_error"/>
</LinearLayout>
adapter:
package com.example.showpicture.showLocalPicture.adapter;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.showpicture.R;
import com.squareup.picasso.Picasso;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements AdapterView.OnItemClickListener {
private static final String TAG = RecyclerViewAdapter.class.getSimpleName();
private Context mContext;
private List<String> mImgList;
private LayoutInflater inflater;
public RecyclerViewAdapter(Context context, List<String> imgList){
this.mContext = context;
this.mImgList = imgList;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.adapter_recycler_view,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder() called with: holder = [" + holder + "], position = [" + mImgList.get(position) + "]");
holder.imageView.setImageURI(Uri.parse(mImgList.get(position)));
}
@Override
public int getItemCount() {
return mImgList.size();
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
class ViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
public ViewHolder(View view) {
super(view);
imageView = view.findViewById(R.id.img_local);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img_local"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
二、网络图片显示
fragment:
用glide或者picasso都可以,用glide的时候需要申请requestopteins
package com.example.showpicture.showNetWorkPicture;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.showpicture.R;
import com.squareup.picasso.Picasso;
public class ShowNetWorkPictureFragment extends Fragment {
private static final String TAG = ShowNetWorkPictureFragment.class.getSimpleName();
private Context mContext;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_network,container,false);
}
@Override
public void onStart() {
super.onStart();
mContext = getContext();
checkNeedPermissions();
initView();
}
private void checkNeedPermissions(){
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//多个权限一起申请
ActivityCompat.requestPermissions(getActivity(), new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
}, 1);
}
}
private void initView() {
Log.d(TAG, "initView() called");
ImageView imageView = getView().findViewById(R.id.img_net);
RequestOptions options = new RequestOptions().centerCrop()
.placeholder(R.drawable.abc_vector_test)
.error(R.drawable.ic_launcher_background)
.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.NONE);
// 用glide或者picasso都可以,用glide的时候需要申请requestopteins Glide.with(mContext).load("https://alifei04.cfp.cn/creative/vcg/veer/1600water/veer-303764513.jpg")
// .apply(options)
// .into(imageView);
Picasso.with(mContext).load("https://alifei04.cfp.cn/creative/vcg/veer/1600water/veer-303764513.jpg")
.resize(300, 300)
.into(imageView);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img_net"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="16dp" />
</LinearLayout>