如何从一个活动中选择图像从firebase获取第二个活动

时间:2018-05-20 09:22:18

标签: android image firebase null bundle

我是Android开发的新手我希望通过从第一个活动中获取所选图像来显示另一个片段中的图像但我收到错误

这是我的网格视图,用户选择图像

$(this)

这是所选图像应该查看

@Override
public void onGridImageSelected(Photo photo, int activityNumber) {
    Log.d(TAG, "onGridImageSelected: selected an image gridview: " + photo.toString());
   FullScreenProductFragment fragment = new FullScreenProductFragment();
    Bundle args = new Bundle();
    args.putParcelable(getString(R.string.photo), photo);
    args.putInt(getString(R.string.activity_number), activityNumber);
    fragment.setArguments(args);
    FragmentTransaction transaction  = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.Profilecontainer, fragment);
    transaction.addToBackStack(getString(R.string.view_post_fragment));
    transaction.commit();
}

这是我的日志

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragmentfullscreenlayout, container, false);
    mImageView = view.findViewById(R.id.zoom_image);

    Bundle bundle = getActivity().getIntent().getExtras();

    UnivarsalImageLoader.setImage(getPhotoFromBundle().getImage_path(),mImageView, null, "");
    Log.d(TAG, "onCreateView: getphoto from bundle"+getPhotoFromBundle().getImage_path());

    return view;
}
private Photo getPhotoFromBundle(){
    Log.d(TAG, "getPhotoFragment: argumentd"+getArguments());
    Bundle bundle = this.getArguments();

    if (bundle != null){
        return bundle.getParcelable(getString(R.string.photo));
    }else {
        return null;
    }
}

当我在片段中记录这个getphotobundle时,我没有得到任何问题,请告诉我

java.lang.NullPointerException: Attempt to invoke virtual method  'java.lang.String com.example.alpha.lapid.models.Photo.getImage_path()' on a null object reference
    at com.example.alpha.lapid.Utils.FullScreenProductFragment.onCreateView(FullScreenProductFragment.java:56)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.j va:1428)

2 个答案:

答案 0 :(得分:1)

Bundle bundle = getActivity().getIntent().getExtras();
Photo photo = getPhotoFromBundle(bundle);
if (photo==null) {
   Toast( ... Photo is null ..);
   return;
}

String path = photo.getImage_path();

File file = new File(path);

if (!file.exists()) {
   Toast( .. file does not exist ..);
    return;
 }


 UnivarsalImageLoader.setImage(file.getAbsolutePath(), mImageView, null, "");

答案 1 :(得分:0)

我通过在我的主要活动中调用它来解决我是在调用片段 如果你想获得所选图像的所有照片细节,你可以使用这个代码,你想要单个图像使用我的朋友greenapps代码

public class Profile_Activity extends AppCompatActivity implements
    ProfileFragment.OnGridImageSelectedListner ,
    ViewPostFragment.OnCommentThreadSelectedListener,
    ViewProfileFragment.OnGridImageSelectedListner,

ViewPostFragment.Ontiemlistner {

private static final String TAG = "ProfileActivity";
@Override
public void ontimlistner(Photo photo) {
    Bundle args = new Bundle();
    FullScreenProductFragment fragment = new FullScreenProductFragment();
    args.putParcelable("large", photo);
    fragment.setArguments(args);
    FragmentTransaction transaction  = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.full_screen_container, fragment);
    transaction.addToBackStack(getString(R.string.fragment_full_screen_product));
    transaction.commit();
}
@Override
public void onCommentThreadSelectedListener(Photo photo) {
    ViewCommetFragment fragment = new ViewCommetFragment();
    Bundle args = new Bundle();
    args.putParcelable(getString(R.string.photo), photo);
    fragment.setArguments(args);
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.Profilecontainer, fragment);
    transaction.addToBackStack(getString(R.string.view_comments_fragment));
    transaction.commit();
}
@Override
public void onGridImageSelected(Photo photo, int activityNumber) {
    Log.d(TAG, "onGridImageSelected: selected an image gridview: " + photo.toString());
    ViewPostFragment fragment = new ViewPostFragment();

    Bundle args = new Bundle();
    args.putParcelable(getString(R.string.photo), photo);
    args.putInt(getString(R.string.activity_number), activityNumber);
    fragment.setArguments(args);
    FragmentTransaction transaction  = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.Profilecontainer, fragment);
    transaction.addToBackStack(getString(R.string.view_post_fragment));
    transaction.commit();
}

我在片段中调用它,我希望该图像显示

public FullScreenProductFragment(){
super();
setArguments(new Bundle());
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable      ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragmentfullscreenlayout,    container, false);
    mImageView = view.findViewById(R.id.zoom_image);
    Bundle bundle = this.getArguments();


     photo = getPhotoFromBundle();

    setProduct();



    return view;
}

 public Photo getPhotoFromBundle(){
    Log.d(TAG, "getPhotoFragment: argumentd"+getArguments());
    Bundle bundle = this.getArguments();
    if (bundle != null){
        return bundle.getParcelable("large");
    }else {
        return null;
    }
   }

此片段中的代码,我在该网格视图上添加了点击侦听器

 public  interface  Ontiemlistner{
 void ontimlistner(Photo photo);
 }
 Ontiemlistner ontiemlistner;
 public ViewPostFragment(){
 super();
 setArguments(new Bundle());
 }


added this code in my gridon item click listener 

 Photo = mPhoto;
 ontiemlistner.ontimlistner(mPhoto);