0

In a fragment,I am having the view pager(using fragmentPageAdapter) and recycler views to display the images.I have option to take the pictures and add to both of them(viewpager and recycler),

During oncreateView of the fragment,adapter has been created with predefined set of images,when i try to access the photos from album location i am trying to use getActivity.getContentResolver() to get Photos,But getActivity() itself returns null post the launch of the photoapplication,i think the problem happens with detach & attach of the fragment,How to handle this scenarios?

OncreateView of fragment

    This Fragments extends basefragment which contains camera launch and Saves it in PhotoGallery

   LinearLayoutManager layoutManager
            = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);

    mRecyclerView = (RecyclerView)v. findViewById(R.id.recycler_view);
    adapter  = new MyRecyclerAdapter(getActivity(),mJobSiteAlbum,getGalleryThumbnailViewCallBackListener);
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setAdapter(adapter);

Within Adapter

    public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
    MyObject photoObject = MyObjects.get(i);
    if(photoObject.getresId() == MyObject.LOCAL_IMAGE){

        Bitmap bitmap = mBaseFragment.getImageFromGallery(photoObject.getSelectedUri());//trying to retrive image from Gallery 
        if(bitmap!=null){
            Log.v("Bitmap>>>Wid",""+bitmap.getWidth());
            Log.v("Bitmap>>>Ht",""+bitmap.getHeight());
        }

        customViewHolder.imageView.setImageBitmap(bitmap);
        customViewHolder.imageView.getLayoutParams().height = 100;
        customViewHolder.imageView.getLayoutParams().width = 100;
        customViewHolder.imageView.requestLayout();



    }else   if(photoObject.getresId() == MyObject.WEB_IMAGE){
        customViewHolder.imageView.setImageUrl(photoObject.getthumbnailimageUrl(), loader);
    }
    customViewHolder.imageView.setOnClickListener(clickListener);
    customViewHolder.imageView.setTag(customViewHolder);

}

//This Method is frm basefragment
public  Bitmap getImageFromGallery(Uri selectedImageUri){
    Bitmap defaultImageBitmap = null;
    try {
        Log.v("URI VAL",
                "selectedImageUri = " + selectedImageUri.toString());
        String mselectedImagePath = getPath(selectedImageUri);
        Log.v("URI VAL",
                "mselectedImagePath = " + mselectedImagePath);
        if (mselectedImagePath != null) {
            System.out.println("local image");
            String[] projection = {MediaStore.MediaColumns.DATA};
            CursorLoader cursorLoader = new CursorLoader(getOurActivity(),
                    selectedImageUri, projection, null, null, null);
            Cursor cursor = cursorLoader.loadInBackground();
            String selectedImagePath;
            if (cursor == null) {
                selectedImagePath = selectedImageUri.getPath();
            } else {
                cursor.moveToFirst();
                int column_index = cursor
                        .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                selectedImagePath = cursor.getString(column_index);
                cursor.close();
            }
            Bitmap bm;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(selectedImagePath, options);
            final int REQUIRED_SIZE = 200;
            int scale = 1;
            while (options.outWidth / scale / 2 >= REQUIRED_SIZE
                    && options.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;
            options.inSampleSize = scale;
            options.inJustDecodeBounds = false;
            defaultImageBitmap = BitmapFactory.decodeFile(selectedImagePath, options);
        } else {
            System.out.println("picasa image!");
            defaultImageBitmap = loadPicasaImageFromGallery(selectedImageUri);
        }
    }catch (Exception e){
        System.out.println("Exception URI!"+e.getMessage());

        defaultImageBitmap = getImageFromURI(selectedImageUri);
    }
    return defaultImageBitmap ;

    public String getPath(Uri uri) {

    String[] projection = { MediaStore.MediaColumns.DATA };
    Cursor  cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);//This GetActivity returns null post launch of the gallery application

    //Do Something to get Path from Cursor
    }
}
}
Rakesh
  • 14,997
  • 13
  • 42
  • 62
  • How do u think, will anyone be able to help you without seeing your code? – Andrey E Dec 23 '15 at 08:05
  • Looks like you are calling getActivity before onAttach were called. But it's impossible to say something concrete, cause code you posted is incomplete. – Andrey E Dec 23 '15 at 09:13

0 Answers0