Android将谷歌图库选择器更改为普通图库选取器

时间:2016-07-04 23:30:14

标签: java android android-gallery

我找到了一个关于图库选取器的代码,它从谷歌图片中挑选照片。但我想从普通画廊中挑选照片。这是代码:

public class ImageCrop {
    private static final String TAG = "ImageCrop";

    private static final boolean REMOVE_TEMP_FILE = true;
    private static final boolean DEBUG = false;
    Context context;
    public static final int PICK_FROM_CAMERA = 0;
    public static final int PICK_FROM_ALBUM = 1;
    public static final int PERMISSION_FROM_CAMERA = 3;

    private static final int PHOTO_SIZE = 1000;
    private static final String ACTIVITY_NAME_PHOTOS = "com.google.android.apps.photos";
    private static final String ACTIVITY_NAME_PLUS = "com.google.android.apps.plus";

    private static boolean mUseActivityPhoto = false;
    private static boolean mUseActivityPlus = false;

    private static Uri mImageCaptureUri;
    private static Bitmap mCropBitmap;
    private static String mTempImagePath;

    private static int mLastAction = PICK_FROM_CAMERA;

    private static final String CAMERA_TEMP_PREFIX = "camera_";
    private static final String CROP_TEMP_PREFIX = "crop_";
    private static final String IMAGE_EXT = ".png";

    public static void checkPackages(Activity context, Intent intentPhoto) {

        final PackageManager pm = context.getPackageManager();
        final Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        mUseActivityPhoto = false;
        mUseActivityPlus = false;

        final List<ResolveInfo> infos = pm.queryIntentActivities(intent, PackageManager.MATCH_ALL);


        for (ResolveInfo info : infos) {
            if(info.activityInfo.packageName.equals(ACTIVITY_NAME_PHOTOS)) {

                final List<ResolveInfo> photoInfos = pm.queryIntentActivities(intentPhoto, PackageManager.MATCH_ALL);
                for (ResolveInfo photoInfo : photoInfos) {
                    if(photoInfo.activityInfo.packageName.equals(ACTIVITY_NAME_PHOTOS)) {
                        Log.d(TAG,"mUseActivityPhoto TRUE");
                        mUseActivityPhoto = true;
                        break;
                    }
                }

            }
            else if(info.activityInfo.packageName.equals(ACTIVITY_NAME_PLUS)) {

                final List<ResolveInfo> photoInfos = pm.queryIntentActivities(intentPhoto, PackageManager.MATCH_ALL);
                for (ResolveInfo photoInfo : photoInfos) {
                    if(photoInfo.activityInfo.packageName.equals(ACTIVITY_NAME_PLUS)) {
                        Log.d(TAG,"mUseActivityPlus TRUE");
                        mUseActivityPlus = true;
                        break;
                    }
                }
            }
        }
    }

    public static void takeCameraAction(Activity context) {
        if(DEBUG)
            Log.d(TAG, "takeCameraAction");
        if (ImageCrop.checkPermissions(context)) {
            ImageCrop.doTakeCameraAction(context);
        } else {
            mLastAction = ImageCrop.PICK_FROM_CAMERA;
        }

    }
    public static void takeAlbumAction(Activity context) {
        if(DEBUG)
            Log.d(TAG, "takeAlbumAction");
        if(ImageCrop.checkPermissions(context)) {
            ImageCrop.doTakeAlbumAction(context);
        }
        else {
            mLastAction = ImageCrop.PICK_FROM_ALBUM;
        }
    }

    private static void doTakeCameraAction(Activity context) {
        if(DEBUG)
            Log.d(TAG, "doTakeCameraAction");

        final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


        final String url = CAMERA_TEMP_PREFIX + String.valueOf(System.currentTimeMillis()) + IMAGE_EXT;
        final File file = new File(Environment.getExternalStorageDirectory(), url);
        mTempImagePath = file.getAbsolutePath();
        mImageCaptureUri = Uri.fromFile(file);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);


        context.startActivityForResult(intent, PICK_FROM_CAMERA);
    }

    private static void doTakeAlbumAction(Activity context) {
        if(DEBUG)
            Log.d(TAG, "doTakeAlbumAction");


        final Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(MediaStore.Images.Media.CONTENT_TYPE);

        checkPackages(context, intent);

        if(mUseActivityPhoto) {
            if(DEBUG)
                Log.d(TAG, "doTakeAlbumAction setPackage ACTIVITY_NAME_PHOTOS");
            intent.setPackage(ACTIVITY_NAME_PHOTOS);
        }
        else if(mUseActivityPlus) {
            if(DEBUG)
                Log.d(TAG, "doTakeAlbumAction setPackage ACTIVITY_NAME_PLUS");
            intent.setPackage(ACTIVITY_NAME_PLUS);
        }
        context.startActivityForResult(intent, ImageCrop.PICK_FROM_ALBUM);
    }

    private static void removeTempFile() {
        // 캡쳐 파일 삭제
        if(mImageCaptureUri != null) {
            final String capturePath = mImageCaptureUri.getPath();
            if(capturePath != null) {
                Log.w(TAG, "removeTempFile capturePath=" + capturePath);

                final File captureFile = new File(capturePath);
                if(captureFile != null) {
                    if (captureFile.getAbsoluteFile().exists()) {
                        captureFile.delete();
                    }
                }
            }
        }


        if(mTempImagePath != null) {
            Log.w(TAG, "removeTempFile mTempImagePath=" + mTempImagePath);

            final File tempFile = new File(mTempImagePath);
            if(tempFile != null) {
                if(tempFile.getAbsoluteFile().exists()) {
                    tempFile.delete();
                }
            }
        }
    }
    private static void removeDataFile(Intent data) {
        if(data == null) {
            Log.w(TAG, "removeDataFile data == null");
            return;
        }
        if(data.getData() == null) {
            Log.w(TAG, "removeDataFile data.getData() == null");
            return;
        }

        final String dataPath = data.getData().getPath();
        if(dataPath == null) {
            Log.w(TAG, "removeDataFile dataPath == null");
            return;
        }
        Log.w(TAG, "removeDataFile dataPath=" + dataPath);

        final File dataFile = new File(dataPath);
        if(dataFile == null) {
            Log.w(TAG, "removeDataFile dataFile == null");
            return;
        }

        if(dataFile.getAbsoluteFile().exists()) {
            dataFile.delete();
        }
    }

    private static File cropFileFromPhotoData(Activity context, Intent data) {
        if(DEBUG)
            Log.d(TAG, "cropFileFromPhotoData");

        if(data.getData() == null) {
            Log.e(TAG, "cropFileFromPhotoData data.getData() == null");
            return null;
        }

        final String dataPath = data.getData().getPath();
        if (dataPath == null) {
            Log.e(TAG, "cropFileFromPhotoData dataPath == null");
            return null;
        }

        File dataFile = null;


        if(dataPath.startsWith("/external")) {
            final Uri dataUri = Uri.parse("content://media"+dataPath);
            final String dataFilePath = getRealPathFromURI(context, dataUri);
            dataFile = new File(dataFilePath);
            boolean exist = dataFile.exists();
            long length = dataFile.length();
            if(DEBUG)
                Log.d(TAG, "cropFileFromPhotoData dataFilePath=" + dataFilePath + " exist="+exist + " length=" +length);
        }
        else {
            dataFile = new File(dataPath);
            boolean exist = dataFile.exists();
            long length = dataFile.length();
            if(DEBUG)
                Log.d(TAG, "cropFileFromPhotoData dataPath=" + dataPath + " exist="+exist + " length=" +length);
        }


        return dataFile;
    }




    public static void pickFromCamera(Activity context, Intent data) {
        if(DEBUG)
            Log.d(TAG, "pickFromCamera => launchCropActivity");

        if(mTempImagePath == null || mTempImagePath.isEmpty()) {
            Log.e(TAG, "pickFromCamera mTempImagePath error");
            return;
        }
}



    public static String getRealPathFromURI(Activity context, Uri contentUri) {
        Cursor cursor = null;
        final String[] proj = { MediaStore.Images.Media.DATA };
        String ret = null;

        try {
            cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
            final int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            ret = cursor.getString(column_index);
        }
        catch(Exception e) {
            Log.e(TAG, "getRealPathFromURI exception");
            return null;
        }

        if (cursor != null) {
            cursor.close();
        }
        return ret;
    }






        if(readEnable && writeEnable && cameraEnable) {
            switch(mLastAction) {
                case ImageCrop.PICK_FROM_CAMERA:
                    if(DEBUG)
                        Log.d(TAG, "doTakeCameraAction");
                    ImageCrop.doTakeCameraAction(context);
                    break;
                case ImageCrop.PICK_FROM_ALBUM:
                    if(DEBUG)
                        Log.d(TAG, "doTakeAlbumAction");
                    ImageCrop.doTakeAlbumAction(context);
                    break;
            }
        }

    }
}

我想用这种方式挑选照片:

   private void selectCover() {

        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(Intent.createChooser(intent, getText(R.string.label_select_img)), SELECT_COVER);
    }

但是我无法将其转换为普通画廊。怎么解决这个?感谢

1 个答案:

答案 0 :(得分:1)

您可以使用它,它只会打开Gallery

public void handleGallery(){
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, REQUEST_CODE_GALLERY);
}

public void handleCamera(){
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    try {
        intent.putExtra("return-data", true);
        startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
    } catch (Exception e) {
        Log.d("error", "cannot take picture", e);
    }
}

然后onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != RESULT_OK) {
        return;
    }
    Bitmap bitmap;

    switch (requestCode) {

        case REQUEST_CODE_GALLERY:
            try {
                Uri uri = data.getData();
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                    Bitmap bitmapScaled = Bitmap.createScaledBitmap(bitmap, 800, 800, true);
                    Drawable drawable=new BitmapDrawable(bitmapScaled);
                    mImage.setImageDrawable(drawable);
                    mImage.setVisibility(View.VISIBLE);
                    mImageString=HelperUtils.encodeTobase64(bitmap);
                } catch (IOException e) {
                    Log.v("galeri", "hata var : "+e.getMessage());
                }
            } catch (Exception e) {
                Log.v("kamera", "hata var : "+e.getMessage());
            }
            break;
        case REQUEST_CODE_TAKE_PICTURE:
            bitmap = (Bitmap) data.getExtras().get("data");
            mImage.setImageBitmap(bitmap);
            mImage.setVisibility(View.VISIBLE);
            mImageString=HelperUtils.encodeTobase64(bitmap);
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}