• LoadImageManager(选择图片,打开照相机)


    package com.k1.doctor.media;
    
    import java.io.File;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Environment;
    import android.provider.MediaStore;
    
    public class LoadImageManager {
        public static final int CHOOSE_IMAGE = 2;
        public static final int IMAGE_CAPTURE = 3;
    
        public static void openImageFile(Activity context) {
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
    
            context.startActivityForResult(intent, CHOOSE_IMAGE);
        }
    
        public static void openTakePhoto(Activity content) {
    
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));
            content.startActivityForResult(intent, IMAGE_CAPTURE);
    
        }
    
        public static String uriToUrl(Context context, Uri uri) {
            String[] proj = { MediaStore.Images.Media.DATA };
    
            Cursor actualimagecursor = ((Activity) context).managedQuery(uri, proj, null, null, null);
    
            int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    
            actualimagecursor.moveToFirst();
    
            String img_path = actualimagecursor.getString(actual_image_column_index);
    
            File file = new File(img_path);
            return file.getPath();
    
        }
    
        public interface CallMethod {
            void back(Object o);
        }
    
    }
    public String getPath() {
            File sdDir = null;
            boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
            if (sdCardExist) {
                sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
                sdDir = new File(sdDir, "voice");
            } else {
                sdDir = new File(Environment.getDataDirectory(), UUID.randomUUID().toString() + ".jpg");
            }
            return sdDir.
    ();
    
        }
     
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            String urls = null;
            if (requestCode == LoadImageManager.CHOOSE_IMAGE) {
                String state = "";
                Uri uri = data.getData();
                String url = LoadImageManager.uriToUrl(getContext(), uri);
                String s = uri.getPath();
                System.out.println(url);
    
                if (s != null) {
                    state = "ok";
                } else {
                    state = "fail";
                }
                urls = getJson(url, state);
                /* 将Bitmap设定到ImageView */
                // Bitmap bitmap = BitmapFactory.decodeFile(url);
                // mFramePick.setVisibility(View.VISIBLE);
                // Drawable d=Drawable.createFromPath(url);
                // mFramePick.setBackground(d);
                System.out.println();
            } else if (requestCode == LoadImageManager.IMAGE_CAPTURE) {
                File picture = new File(getCurrentFilePath());
                Bitmap decodeStream;
                String state = "";
    
                // mFramePick.setVisibility(View.VISIBLE);
                // Drawable d=Drawable.createFromPath(picture.getPath());
                // mFramePick.setBackground(d);
                // 得到图片
                try {
                    decodeStream = BitmapFactory.decodeStream(new FileInputStream(picture));
                    // Drawable d = new BitmapDrawable(decodeStream);
                    // navigationLinear.setBackground(d);
                    state = "ok";
                } catch (FileNotFoundException e) {
                    state = "fail";
                    e.printStackTrace();
                } finally {
    
                    urls = getJson(picture.getPath(), state);
                }
            }
    
        
    
        }
    
    
    
     
    public String getCurrentFilePath() {
            // TODO Auto-generated method stub
            return mCurrentFilePathString;
        }
    
    
    


  • 相关阅读:
    理解MySQL——索引与优化
    Android中shape的使用
    Android之Camera控制拍照
    android的fragments管理
    android的fragment基本介绍
    android的animator
    android软键盘弹出隐藏的监听
    android平板Home键的监听
    android jsonarray
    android 应用静默自启动的解决方法
  • 原文地址:https://www.cnblogs.com/g-sheng/p/5412186.html
Copyright © 2020-2023  润新知