当我在 19 API 中打开我的图片库时,没有看到画廊,但在 23API 中它在 android studio 中显示了画廊?

Posted

技术标签:

【中文标题】当我在 19 API 中打开我的图片库时,没有看到画廊,但在 23API 中它在 android studio 中显示了画廊?【英文标题】:when i open my image gallery in 19 API gallery is not seen but in 23API It shows gallery In android studio? 【发布时间】:2017-06-04 07:25:44 【问题描述】:

当我在 19 API 中打开我的图片库时,没有看到画廊,但在 23API 中它显示了画廊在 android studio 中有什么问题请帮忙。我已经共享了 19 api 和 23 api 的图片。 API19 gallery resultAPI23 gallery result

    public class DocumentsFragment extends Fragment implements View.OnClickListener, DocumentImageEventListener 
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    FragmentDocumentsBinding fragmentDocumentsBinding;
    private int PICK_IMAGE_REQUEST = 1;
    private int PICK_VIDEO_REQUEST = 2;
    private int PICK_MULTIPLE_IMAGE_REQUEST = 3;
    CountDownTimer countDownTimer;
    int currentItemPosition = 0;
    private DocumentImagesAdapter documentImagesAdapter;

    public DocumentsFragment() 
        // Required empty public constructor
    

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment DocumentsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static DocumentsFragment newInstance(String param1, String param2) 
        DocumentsFragment fragment = new DocumentsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        if (getArguments() != null) 
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        fragmentDocumentsBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_documents, container, false);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Media");
        setListeners();
        return fragmentDocumentsBinding.getRoot();
    

    private void setListeners() 
        fragmentDocumentsBinding.selectPhoto.setOnClickListener(this);
        fragmentDocumentsBinding.selectVideo.setOnClickListener(this);

        fragmentDocumentsBinding.video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() 
            @Override
            public void onPrepared(MediaPlayer mp) 
                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() 
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) 
                /*
                 * add media controller
                 */
                        MediaController mc = new MediaController(getActivity());
                        fragmentDocumentsBinding.video.setMediaController(mc);
                /*
                 * and set its position on screen
                 */
                        mc.setAnchorView(fragmentDocumentsBinding.video);
                    
                );
            
        );
    

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
        super.onActivityResult(requestCode, resultCode, data);

//        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) 
//
//            Uri uri = data.getData();
//
//            try 
//                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
//                // Log.d(TAG, String.valueOf(bitmap));
//
//                fragmentDocumentsBinding.image.setImageBitmap(bitmap);
//                fragmentDocumentsBinding.image.setVisibility(View.VISIBLE);
//                fragmentDocumentsBinding.video.setVisibility(View.GONE);
//             catch (IOException e) 
//                e.printStackTrace();
//            
//        
        if (requestCode == PICK_MULTIPLE_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) 
            ArrayList<Uri> images = new ArrayList<>();
            if (data.getClipData() != null) 
                try 
                    for (int i = 0; i < data.getClipData().getItemCount(); i++) 
                        Uri uri = data.getClipData().getItemAt(i).getUri();
//                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
                        images.add(uri);
                    
                    startTimer(data.getClipData().getItemCount());
                 catch (Exception e) 
                    e.printStackTrace();
                
             else if (data.getData() != null) 
                images.add(data.getData());
            
            documentImagesAdapter = new DocumentImagesAdapter(getActivity(), images, this);
            fragmentDocumentsBinding.imagePager.setAdapter(documentImagesAdapter);
            fragmentDocumentsBinding.circleIndicator.setViewPager(fragmentDocumentsBinding.imagePager);
//                fragmentDocumentsBinding.circleIndicator.setOnPageChangeListener(fragmentDocumentsBinding.imagePager);
//                fragmentDocumentsBinding.imagePager.setImageBitmap(bitmap);
            fragmentDocumentsBinding.imagePager.setVisibility(View.VISIBLE);
            fragmentDocumentsBinding.video.setVisibility(View.GONE);
            fragmentDocumentsBinding.noDataMessage.setVisibility(View.GONE);

         else if (requestCode == PICK_VIDEO_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) 
            Uri selectedImageUri = data.getData();

            // OI FILE Manager
            String filemanagerstring = selectedImageUri.getPath();

            // MEDIA GALLERY
            String selectedImagePath = getRealPathFromURI(selectedImageUri);
            fragmentDocumentsBinding.video.setVideoPath(selectedImagePath);
            fragmentDocumentsBinding.imagePager.setVisibility(View.GONE);
            fragmentDocumentsBinding.video.setVisibility(View.VISIBLE);
            fragmentDocumentsBinding.video.start();
            fragmentDocumentsBinding.noDataMessage.setVisibility(View.GONE);
        
    

    void startTimer(final int itemCount) 
        long millisInFuture = (itemCount + 1) * 3000;
        countDownTimer = new CountDownTimer(millisInFuture, 3000) 
            @Override
            public void onTick(long l) 
                fragmentDocumentsBinding.circleIndicator.setCurrentItem(currentItemPosition++);
            

            @Override
            public void onFinish() 
                currentItemPosition = 0;
                startTimer(itemCount);
            
        .start();
    

//    public String getRealPathFromURI(Uri uri) 
//        String[] projection = MediaStore.Video.Media.DATA;
//        Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
//        if (cursor != null) 
//            // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
//            // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
//            int column_index = cursor
//                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
//            cursor.moveToFirst();
//            return cursor.getString(column_index);
//         else
//            return null;
//    

    private String getRealPathFromURI(Uri contentURI) 
        // acquisition
        String[] projection = 
                MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.DURATION,
                MediaStore.Video.Media.DATA
        ;

        Cursor cursor = getActivity().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, MediaStore.Video.Media._ID + "=" + contentURI.getLastPathSegment().split(":")[1], null, null);
        if (cursor == null)  // Source is Dropbox or other similar local file path
            return contentURI.getPath();
         else 
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA);
            return cursor.getString(idx);
        
    

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) 
        if (mListener != null) 
            mListener.onFragmentInteraction(uri);
        
    

    @Override
    public void onAttach(Context context) 
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) 
            mListener = (OnFragmentInteractionListener) context;
         else 
//            throw new RuntimeException(context.toString()
//                    + " must implement OnFragmentInteractionListener");
        
    

    @Override
    public void onDetach() 
        super.onDetach();
        mListener = null;
    

    @Override
    public void onClick(View view) 
        if (view == fragmentDocumentsBinding.selectPhoto) 
            currentItemPosition = 0;
            if (countDownTimer != null)
                countDownTimer.cancel();
            callGalleryForImage();
         else if (view == fragmentDocumentsBinding.selectVideo) 
            currentItemPosition = 0;
            if (countDownTimer != null)
                countDownTimer.cancel();
            callGalleryForVideo();
        
    

    void callGalleryForImage() 
        Intent intent = new Intent();
// Show only images, no videos or anything else
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
// Always show the chooser (if there are multiple options available)
        startActivityForResult(Intent.createChooser(intent, "Select Pictures"), PICK_MULTIPLE_IMAGE_REQUEST);
    

    void callGalleryForVideo() 

        Intent intent = new Intent();
// Show only images, no videos or anything else
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
        startActivityForResult(Intent.createChooser(intent, "Select Video"), PICK_VIDEO_REQUEST);
    

    @Override
    public void onImageClick(View view, Uri uri) 
        Intent imageViewer = new Intent(getActivity(), ImageViewerActivity.class);
        imageViewer.putExtra("imageUri", uri.toString());
        startActivity(imageViewer);
    

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener 
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    

【问题讨论】:

展示你尝试了什么,你是如何打开画廊的,给我们一些关于你做了什么的线索...... 【参考方案1】:

有没有权限问题?

什么是目标API?

如果目标 api >= 23 你应该使用运行时权限

https://developer.android.com/training/permissions/requesting.html

【讨论】:

感谢回复,目标api是24,最小值是9 你可以尝试更改目标api 21

以上是关于当我在 19 API 中打开我的图片库时,没有看到画廊,但在 23API 中它在 android studio 中显示了画廊?的主要内容,如果未能解决你的问题,请参考以下文章

使用ajax在codeigniter中上传图片

为啥我的图片只能在桌面上加载,而不能在移动设备上加载?

当我在我的文本框中输入故事时,它应该点击这个 api

[当我在颤抖中单击TextField时,我的键盘打开和关闭

当我在 post 方法上点击 api 时得到空对象响应

本地主机中的 XAMPP 延迟