从设备库android导入所有图像

时间:2016-11-06 17:00:12

标签: java android

我试图从设备库(而不是SD卡)导入所有图像但没有成功。我正在使用权限功能来使用外部存储。在我确认许可后,什么也没发生。任何人都可以帮忙我错了吗?

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImagesSelect extends AppCompatActivity {

    private static final int SELECT_PICTURE = 1;
    private String selectedImagePath;
    private Cursor cursor;
    private int columnIndex;
    private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_images_select);


        if (ContextCompat.checkSelfPermission(ImagesSelect.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(
                    ImagesSelect.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {

                // Explain to the user why we need to read the contacts

            }
            else{

                ActivityCompat.requestPermissions(ImagesSelect.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
    {

        switch (requestCode){

            case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:{

                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

                    String[] projection = new String[] {
                            MediaStore.Images.Media._ID,
                            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                            MediaStore.Images.Media.DATE_TAKEN
                    };

                    Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    cursor = getContentResolver().query(images,
                            projection,
                            null,
                            null,
                            null
                    );

                    if (cursor.moveToFirst()) {
                        String bucket;
                        String date;
                        int bucketColumn = cursor.getColumnIndex(
                                MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

                        int dateColumn = cursor.getColumnIndex(
                                MediaStore.Images.Media.DATE_TAKEN);
                        do {
                            bucket = cursor.getString(bucketColumn);
                            date = cursor.getString(dateColumn);
                            //Log.i("ListingImages", " bucket=" + bucket + "  date_taken=" + date);

                            GridView galleryGV = (GridView) findViewById(R.id.imagesGridView);
                            galleryGV.setAdapter(new ImageAdapter(this));

                        } while (cursor.moveToNext());

                    }
                }
                else
                {

                }
                return;
            }

        }

    }

    public class ImageAdapter extends BaseAdapter {

        private Context context;

        public ImageAdapter(Context localContext){
            context = localContext;
        }

        public int getCount() {
            return cursor.getCount();
        }

        public Object getItem(int position){
            return position;
        }

        public long getItemId(int position){
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(context);
                cursor.moveToPosition(position);
                int imageID = cursor.getInt(columnIndex);
                imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }
            return imageView;
        }
    }
}

0 个答案:

没有答案