我希望将我的应用相册中的图片显示到带有视图持有者的回收器视图中,但是在我尝试了教程并编译之后。没有错误,但我的应用中都没有显示照片。
仍面临同样的问题,我已更新了当前的代码
30-01-17更新
以下是代码:
Album.java
public class Album extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.album, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.albumView);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(), 3);
recyclerView.setLayoutManager(layoutManager);
ArrayList<CreateList> createLists = prepareData();
AlbumAdpater adapter = new AlbumAdpater(getContext(), createLists);
recyclerView.setAdapter(adapter);
return view;
}
private ArrayList<CreateList> prepareData() {
ArrayList<CreateList> theimage = new ArrayList<>();
String path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString();
File files = new File(path);
File file[] = files.listFiles();
Log.e("Album","PrepareData");
for (int i = 0; i < file.length; i++) {
CreateList createList = new CreateList();
createList.setImage_location(file[i].getAbsolutePath());
createList.setImage_title(file[i].getName());
theimage.add(createList);
}
return theimage;
}
public void reprmission() {if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED);
{
if (ActivityCompat.shouldShowRequestPermissionRationale(
getActivity(), android.Manifest.permission.READ_EXTERNAL_STORAGE)){
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]
{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSION);
}
} return;
}
}
Createlist.java
public class CreateList {
private String image_title;
private Integer image_id;
private String image_location;
public String getImage_location() {
return image_location;
}
public void setImage_location(String image_location) {
this.image_location = image_location;
}
public String getImage_title() {
return image_title;
}
public void setImage_title(String android_version_name) {
this.image_title = android_version_name;
}
public Integer getImage_ID() {
return image_id;
}
public void setImage_ID(Integer android_image_url) {
this.image_id = android_image_url;
}
}
AlbumAdapter.java
public class AlbumAdpater extends RecyclerView.Adapter<AlbumAdpater.ViewHolder> {
private ArrayList<CreateList> galleryList;
private Context context;
public AlbumAdpater(Context context, ArrayList<CreateList> galleryList){
this.galleryList= galleryList;
this.context = context;
}
@Override
public AlbumAdpater.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i){
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_layout, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(AlbumAdpater.ViewHolder viewHolder, int i){
// viewHolder.title.setText(galleryList.get(i).getImage_title());
viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
// viewHolder.img.setImageResource((galleryList.get(i).getImage_ID()));
setImage(galleryList.get(i).getImage_location(),viewHolder.img);
Picasso.with(context).load(galleryList.get(i).getImage_location()).resize(50,50).centerCrop().into(viewHolder.img);
}
@Override
public int getItemCount(){
return galleryList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView title;
private ImageView img;
public ViewHolder(View view){
super(view);
title = (TextView) view.findViewById(R.id.imgtitle);
img = (ImageView) view.findViewById(R.id.img);
}
}
void setImage(String filePath, ImageView imageView){
Picasso.with(context).load(new File(filePath)).into(imageView);
}
}
Logcat报告
Reconstruct Branch :
01-30 13:31:54.924 8612-8701/com.example.kaiser.bdiary I/OpenGLRenderer: Initialized EGL, version 1.4
01-30 13:31:54.941 8612-8612/com.example.kaiser.bdiary W/Settings: Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
01-30 13:31:54.951 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:31:54.952 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:31:54.954 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:31:54.956 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:31:54.957 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:31:54.958 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:32:23.922 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@b911fe5
01-30 13:32:23.979 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@adce0c8
01-30 13:33:41.293 8612-8695/com.example.kaiser.bdiary W/System: ClassLoader referenced unknown path: /data/data/com.example.kaiser.bdiary/lib
01-30 13:33:41.349 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@d54d109
01-30 13:33:41.350 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@ac3ef0e
01-30 13:33:41.350 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@b173d2f
01-30 13:33:41.387 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@7a4e0bc
01-30 13:33:41.387 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@b152845
01-30 13:33:41.390 8612-8612/com.example.kaiser.bdiary E/Album: PrepareData
01-30 13:33:41.393 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@3d4ca8
01-30 13:33:41.535 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:33:41.536 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:33:41.538 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:33:41.543 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:33:41.545 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:33:41.547 8612-8612/com.example.kaiser.bdiary E/Album: onBindViewHolder
01-30 13:33:41.579 8612-8701/com.example.kaiser.bdiary V/RenderScript: 0x7f819bb000 Launching thread(s), CPUs 4
01-30 13:33:50.192 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@fcf2205
01-30 13:33:50.916 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@b720c8b
01-30 13:33:53.217 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@724d981
01-30 13:33:54.881 8612-8612/com.example.kaiser.bdiary V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@fbc1a26
RecycleView
<?xml version="1.0" encoding="utf-8"?<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/albumView">
</android.support.v7.widget.RecyclerView>
我一直在使用手机作为模拟器,当我使用工作室本身的模拟器时,它会崩溃,因为数组的长度为空。我以为我已经填充了数组列表了?不?
新错误
01-30 13:48:55.551 4603-4603/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kaiser.bdiary, PID: 4603
java.lang.NullPointerException: Attempt to get length of null array
at com.example.kaiser.bdiary.Album.prepareData(Album.java:44)
at com.example.kaiser.bdiary.Album.onCreateView(Album.java:29)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
at android.view.View.measure(View.java:18788)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:2)
由于READ_EXTERNAL_STORAGE是危险权限,您应该在运行时请求该权限。请检查documentation.也不要忘记在manifest
中声明。
您的照片无法显示,因为您要在image_location
中分配Activity
,而在适配器中,您使用 null 的image_id
设置图像。您没有获得 NPE ,因为您创建了一个空的ArrayList
并且没有推送任何Object
。
在ArrayList
中添加数据应该是这样的
private ArrayList<CreateList> prepareData() {
ArrayList<CreateList> theimage = new ArrayList<>();
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
.toString();
File f = new File(path);
File file[] = f.listFiles();
Log.e("Album","PrepareData");
for (int i = 0; i < file.length; i++) {
CreateList createList = new CreateList();
createList.setImage_location(file[i].getAbsolutePath());
createList.setImage_title(file[i].getName());
//If you want to use image_id then you have to set a value using setImage_ID here
theimage.add(createList);
}
return theimage;
}
等任何库在适配器中显示图片
使用毕加索
Picasso.with(context).load(galleryList.get(i).getImage_location()).resize(50, 50).centerCrop().into(viewHolder.img);
使用Glide
Glide.with(context).load(galleryList.get(i).getImage_location()).centerCrop().placeholder(R.drawable.placeholder_img).into(viewHolder.img);
答案 1 :(得分:1)
管理以获得Kaushik先生的帮助
Album.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
imgview = (ImageView) getActivity().findViewById(R.id.solopic);
soloimg = (LinearLayout)getActivity().findViewById(R.id.imgsolo);
View view = inflater.inflate(R.layout.album, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.albumView);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(), 3);
recyclerView.setLayoutManager(layoutManager);
if (Build.VERSION.SDK_INT >= 23) {
if (!verifyGalleryPermission()) {
ArrayList<CreateList> createLists = prepareData();
AlbumAdpater adapter = new AlbumAdpater(getContext(), createLists);
recyclerView.setAdapter(adapter);
}
} else {
ArrayList<CreateList> createLists = prepareData();
AlbumAdpater adapter = new AlbumAdpater(getContext(), createLists);
recyclerView.setAdapter(adapter);
}
return view;
}
private ArrayList<CreateList> prepareData() {
ArrayList<CreateList> theimage = new ArrayList<>();
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()+"/BDiary";
File files = new File(path);
if(files.exists()){
File file[] = files.listFiles();
Log.e("Album","PrepareData");
for (File aFile : file) {
CreateList createList = new CreateList();
createList.setImage_location(aFile.getAbsolutePath());
createList.setImage_title(aFile.getName());
theimage.add(createList);
}
}
else
{
Toast.makeText(getContext(),"Folders not found", Toast.LENGTH_SHORT).show();
}
return theimage;
}
@TargetApi(23)
private boolean verifyGalleryPermission() {
if (ActivityCompat.checkSelfPermission(getActivity(),android.Manifest.permission.READ_EXTERNAL_STORAGE)
!=
PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(android.Manifest.permission.READ_EXTERNAL_STORAGE))
{
displayPermissionDialog("Would like to grant BDiary access to read your gallery",
new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION);
} else
{
requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION);
}
return true;
}
return false;
}
@TargetApi(23)
private void displayPermissionDialog(String msg, final String[] permission, final int resultCode) {
AlertDialog alertDialog = new AlertDialog
.Builder(getActivity())
.setMessage(msg)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
requestPermissions(permission, resultCode);
}
}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.create();
alertDialog.show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSION:
ArrayList<CreateList> createLists = prepareData();
AlbumAdpater adapter = new AlbumAdpater(getContext(), createLists);
recyclerView.setAdapter(adapter);
break;
}
}
}
AlbumAdapter.java
公共类AlbumAdpater扩展了RecyclerView.Adapter {
private ArrayList<CreateList> galleryList;
private Context context;
public AlbumAdpater(Context context, ArrayList<CreateList> galleryList){
this.galleryList= galleryList;
this.context = context;
}
@Override
public AlbumAdpater.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i){
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_layout, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(AlbumAdpater.ViewHolder viewHolder, int i){
viewHolder.title.setText(galleryList.get(i).getImage_title());
viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(context).load(galleryList.get(i).getImage_location()).centerCrop().into(viewHolder.img);
}
@Override
public int getItemCount(){
return galleryList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView title;
private ImageView img;
public ViewHolder(View view){
super(view);
title = (TextView) view.findViewById(R.id.imgtitle);
img = (ImageView) view.findViewById(R.id.img);
}
}
}
希望这也会帮助那些正在做画廊的人=) 干杯! 这必须归功于Kaushik先生
答案 2 :(得分:0)
1)首先,确保图片目录中有图像文件。
2)您没有在列表项上添加文件路径。你只需添加文件的名称,而不是将其添加到列表中。
for (int i = 0; i < file.length; i++) {
CreateList createList = new CreateList();
// createList.setImage_location(file[i].getName());
createList.setImage_location(file[i].getPath);
theimage.add(createList);
}
3)您没有在适配器类上将文件路径绑定到imageview。
4)在你的适配器类上添加以下方法。
void setImage(String filePath,ImageView imageView){//here I made the mistake. Apologise.
Picasso.with(context).load(new File(filePath)).into(imageView);
}
5)从onBindViewHolder(..)方法做下面的事情。
@Override
public void onBindViewHolder(AlbumAdpater.ViewHolder viewHolder, int i){
.....
......
setImage(galleryList.get(i).getImage_location(),viewHolder.img);
}
<强>更新强>
我使用Picasso库加载文件,因为它将管理outOfMemoryError。所以,在这里,如果你不想毕加索图书馆的意思, 使用以下方法。
Uri uri= Uri.parse(filePath);
imageView.setImageUri(uri);