java– 需要从相机和图库中选择代码的图像 – 适用于所有Android手机

java– 需要从相机和图库中选择代码的图像 – 适用于所有Android手机,第1张

概述privatevoidselectImage(){finalCharSequence[]items={"TakePhoto","ChoosefromLibrary","Cancel"};AlertDialog.Builderbuilder=newAlertDialog.Builder(PostProperty2Activity.this)

private voID selectimage() {    final CharSequence[] items = { "Take Photo", "Choose from library",            "Cancel" };    AlertDialog.Builder builder = new AlertDialog.Builder(            PostProperty2Activity.this);    builder.setTitle("Add Photo!");    builder.setItems(items, new DialogInterface.OnClickListener() {        @OverrIDe        public voID onClick(DialogInterface dialog, int item) {            if (items[item].equals("Take Photo")) {                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                file f = new file(androID.os.Environment                        .getExternalStorageDirectory(), "temp.jpg");                Log.d("file f ", "" + Uri.fromfile(f));                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromfile(f));                startActivityForResult(intent, REQUEST_CAMERA);            } else if (items[item].equals("Choose from library")) {                Intent intent = new Intent(                        Intent.ACTION_PICK,                        androID.provIDer.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                Bitmap.CompressFormat.JPEG.toString();                intent.setType("image/*");                startActivityForResult(                        Intent.createChooser(intent, "Select file"),                        SELECT_file);            } else if (items[item].equals("Cancel")) {                dialog.dismiss();            }        }    });    builder.show();}@OverrIDeprotected voID onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (resultCode == RESulT_OK) {        if (requestCode == REQUEST_CAMERA) {            file f = new file(Environment.getExternalStorageDirectory()                    .toString());            Log.d("file fd ", "" + Uri.fromfile(f));            Log.d("getactivity f ", "" + f);            Log.d("fListfile f ", "" + f.Listfiles());            for (file temp : f.Listfiles()) {                if (temp.getname().equals("temp.jpg")) {                    f = temp;                    break;                }            }            try {                Bitmap bm;                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();                bm = BitmapFactory.decodefile(f.getabsolutePath(),                        btmapOptions);                // bm = Bitmap.createScaledBitmap(bm, 70, 70, true);                pdialog.show();                profileimage.setimageBitmap(bm);                String path = androID.os.Environment                        .getExternalStorageDirectory()                        + file.separator                        + "Upostr";                f.delete();                file wallpaperDirectory = new file(path);                wallpaperDirectory.mkdirs();                OutputStream fOut = null;                file file = new file(wallpaperDirectory,                        String.valueOf(System.currentTimeMillis()) + ".jpg");                imagepath = file.getabsolutePath();                try {                    fOut = new fileOutputStream(file);                    bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);                    fOut.flush();                    fOut.close();                } catch (fileNotFoundException e) {                    e.printstacktrace();                } catch (IOException e) {                    e.printstacktrace();                } catch (Exception e) {                    e.printstacktrace();                }            } catch (Exception e) {                e.printstacktrace();            }            imagevalue = "image";            uploadImagetoServer();            // upload(imagepath);        } else if (requestCode == SELECT_file) {            Uri selectedImageUri = data.getData();            Log.d("uri", "" + selectedImageUri);            String tempPath = getPath(selectedImageUri);            Bitmap bm;            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();            bm = BitmapFactory.decodefile(tempPath, btmapOptions);            pdialog.show();            profileimage.setimageBitmap(bm);            imagepath = getRealPathFromURI(selectedImageUri);            imagevalue = "image";            uploadImagetoServer();            // upload(imagepath);        }    }}

代码适用于smasung或sony,但不适用于intex和其他一些AndroID设备.如果我在此代码中添加裁剪,它也会在sony中崩溃…

解决这个问题的任何方法?

解决方法:

if (options[item].equals("Choose from gallery")) {                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {                    Intent photopickerIntent = new Intent(Intent.ACTION_OPEN_document, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                    photopickerIntent.setType("image/*");                    photopickerIntent.addcategory(photopickerIntent.category_OPENABLE);                    startActivityForResult(photopickerIntent, 2);                } else if (options[item].equals("Cancel")) {                    dialog.dismiss();                } else {                    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                    img_path = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString();                    startActivityForResult(intent, 2);                }            }

总结

以上是内存溢出为你收集整理的java – 需要从相机和图库中选择代码的图像 – 适用于所有Android手机全部内容,希望文章能够帮你解决java – 需要从相机和图库中选择代码的图像 – 适用于所有Android手机所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存