android 判断获取相册图片是横向还是纵向

android 判断获取相册图片是横向还是纵向,第1张

// 从本地选择(比如按钮触发该事件,调取系统相册) if (EnvironmentgetExternalStorageState()equals( EnvironmentMEDIA_MOUNTED)) { Intent getImageIntent = new Intent(IntentACTION_GET_CONTENT); getImageIntentaddCategory(IntentCATEGORY_OPENABLE); getImageIntentsetType("image/jpeg"); startActivityForResult(getImageIntent, 4); } else { ToastmakeText(getApplicationContext(), "SD卡不可用", 0)show(); } // 本地选择 结果在activity的onactivityresult()中写:if (data != null) {//判空 Uri uri = datagetData(); urigetPath()toString(); ContentResolver cr = thisgetContentResolver(); try { Bitmap bmp;//压缩 BitmapFactoryOptions option = new BitmapFactoryOptions(); optioninJustDecodeBounds = true; optioninPurgeable = true; optioninInputShareable = true; optioninSampleSize = 2; optioninJustDecodeBounds = false; bmp = BitmapFactorydecodeStream(cropenInputStream(uri), null, option);//imageView_userLogo(view控件设置背景) imageView_userLogosetImageBitmap(BmpToRound toRoundBitmap(bmp)); File file = SaveBitmapToSdCardsaveMyBitmap( getApplicationContext(), "" + SystemcurrentTimeMillis(), bmp); startUpLoad(file); } catch (FileNotFoundException e) { eprintStackTrace(); } }

Intent intent = new Intent(IntentACTION_GET_CONTENT);

intentsetType("image/");

startActivityForResult(intent, 1);

由于业务需要,APP内嵌H5,需要调去系统相册和拍照,网上找了点资料,整理一下,供大家参考:

private static final int REQUEST_CAMERA =1;

private static final int REQUEST_CHOOSE =2;

private ValueCallbackmUploadMessage;

private ValueCallbackmUploadMessagesAboveL;

private UricameraUri;

//50以后的方法

webViewsetWebChromeClient(new WebChromeClient() {

@RequiresApi(api = BuildVERSION_CODESLOLLIPOP)

@Override

    public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {

if (mUploadMessagesAboveL !=null) {

mUploadMessagesAboveLonReceiveValue(null);

            mUploadMessagesAboveL =null;

        }else {

mUploadMessagesAboveL = filePathCallback;

            selectImage();

        }

return true;

    }

});

//选择和拍照,对应的string文件,可以自己写死:拍照,相册,取消

private void selectImage() {

ActionSheetcreateBuilder(this, getSupportFragmentManager())

setOtherButtonTitles(new String[]{getResources()getString(Rstringcommon_tip_photos), getResources()getString(Rstringcommon_picture)})

setCancelButtonTitle(getResources()getString(Rstringcommon_cancel))setCancelableOnTouchOutside(true)setListener(new ActionSheetActionSheetListener() {

public void onDismiss(ActionSheet actionSheet, boolean isCancel) {

if (mUploadMessage !=null) {

mUploadMessageonReceiveValue(null);

                mUploadMessage =null;

            }

if (mUploadMessagesAboveL !=null) {

mUploadMessagesAboveLonReceiveValue(null);

                mUploadMessagesAboveL =null;

            }

actionSheetdismiss();

        }

@RequiresApi(api = BuildVERSION_CODESM)

public void onOtherButtonClick(ActionSheet actionSheet, int index) {

switch (index) {

case 0:

chosePicture();

break;

                case 1:

setRequestCamera();

            }

}

})show();

}

//拍照,添加权限申请  这个可以自己写下,我这边是项目中写好的,直接拿过来用了

public void setRequestCamera() {

permissionsBuilder =new YXTPermissionsBuilderBuilder(this)

setOnGrantedListener((requestCode, perms) -> {

openCamera();

            })

setRationale4NeverAskAgain(LanguageUtilsisEnglish() Stringformat(getString(Rstringpermission_tips), getString(Rstringcommon_camera), getString(Rstringapp_name), getString(Rstringapp_name)) :

Stringformat(getString(Rstringpermission_tips), getString(Rstringapp_name), getString(Rstringcommon_camera), getString(Rstringapp_name)))

//必需

            setRequestCode(ConstantsDataGET_CAMERA)

build();

    permissionsBuilderrequestPermissions(ManifestpermissionCAMERA);

}

/

本地相册选择

/

private void chosePicture() {

Intent innerIntent =new Intent(IntentACTION_GET_CONTENT, androidproviderMediaStoreImagesMediaEXTERNAL_CONTENT_URI);

    innerIntentsetType("image/");

    Intent wrapperIntent = IntentcreateChooser(innerIntent, null);

    startActivityForResult(wrapperIntent, REQUEST_CHOOSE);

}

/

打开照相机

/

private void openCamera() {

Intent intent =new Intent(MediaStoreACTION_IMAGE_CAPTURE);

    String imagePaths = EnvironmentgetExternalStorageDirectory()getPath() +"/pbccrc/Images/" + (SystemcurrentTimeMillis() +"jpg");

    // 必须确保文件夹路径存在,否则拍照后无法完成回调

    File vFile =new File(imagePaths);

    if (!vFileexists()) {

File vDirPath = vFilegetParentFile();

        vDirPathmkdirs();

    }else {

if (vFileexists()) {

vFiledelete();

        }

}

cameraUri = FileProvidergetUriForFile(

this,

            getPackageName() +"fileprovider",

            vFile);

    intentputExtra(MediaStoreEXTRA_OUTPUT, cameraUri);

    startActivityForResult(intent, REQUEST_CAMERA);

}

/

选择照片后结束

@param data

/

private UriafterChosePic(Intent data) {

if (data !=null) {

final String path = datagetData()getPath();

        if (path !=null && (pathendsWith("png") || pathendsWith("PNG") || pathendsWith("jpg") || pathendsWith("JPG"))) {

return datagetData();

        }else {

ToastmakeText(this, "上传的仅支持png或jpg格式", ToastLENGTH_SHORT)show();

        }

}

return null;

}

/

返回文件选择

/

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

if (mUploadMessagesAboveL !=null) {

onActivityResultAboveL(requestCode, resultCode, intent);

    }

if (mUploadMessage ==null)return;

    Uri uri =null;

    if (requestCode ==REQUEST_CAMERA && resultCode ==RESULT_OK) {

uri =cameraUri;

        Loge("onActivityResult: " + uritoString());

    }

if (requestCode ==REQUEST_CHOOSE && resultCode ==RESULT_OK) {

uri = afterChosePic(intent);

    }

mUploadMessageonReceiveValue(uri);

    mUploadMessage =null;

    superonActivityResult(requestCode, resultCode, intent);

}

/

50以后机型 返回文件选择

@param requestCode

@param resultCode

@param data

/

private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) {

Uri[] results =null;

    if (requestCode ==REQUEST_CAMERA && resultCode ==RESULT_OK) {

results =new Uri[]{cameraUri};

    }

if (requestCode ==REQUEST_CHOOSE && resultCode ==RESULT_OK) {

if (data !=null) {

String dataString = datagetDataString();

            if (dataString !=null)

results =new Uri[]{Uriparse(dataString)};

        }

}

mUploadMessagesAboveLonReceiveValue(results);

    mUploadMessagesAboveL =null;

return;

}

基本都可以拿去用了,希望有帮助

Intent intent = new Intent(IntentACTION_PICK);

intentsetType("image/");

// 开启一个带有返回值的Activity,请求码为100

startActivityForResult(intent, 100);

Bitmap bitmap = BitmapFactorydecodeFile(String path);

linearlayoutsetBackground(new BitmapDrawable(bitmap));

调用startActivityforResult(intent);方法启动intent跳转 然后在Activity 里面覆写 onActivityResult();方法。在这个方法里面接收跳转出去返回来的结果,通过结果吗code能够获取

以上就是关于android 判断获取相册图片是横向还是纵向全部的内容,包括:android 判断获取相册图片是横向还是纵向、android怎么调用系统自带的图库打开指定目录的相册、Android WebView 调用系统拍照和相册等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9330566.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存