您现在的位置: 爱51代码网 >> 范文 >> 文章正文
Android 2.3.5系统内置图片Gallery程序无法正常调用

Android 2.3.5系统内置图片Gallery程序无法正常调用

之前开发了一个图片功能,就是即可以拍照,又可以直接调用系统内置Gallery程序选择图片,然后将图片显示在自己的程序ImageView中。
该功能在华为T8620可以正常使用(Android4.0),但是最近试了另一款康佳的v926(Android 2.3.5),却无法从Gallery的图片选择界面中选中的图片显示在ImageView中(会一直让你选,没反应),测试代码发现竟然不肯回调函数 onActivityResult()!  所以只能到这来请教大牛们了,望指点~~
主要代码:
Java code?private void doPickPhotoAction() {           Context context = PicPostActivity.this;          // Wrap our context to inflate list items using correct theme           final Context dialogContext = new ContextThemeWrapper(context,                   android.R.style.Theme_Light);           String cancel="返回";           String[] choices;           choices = new String[2];         choices[0] = getString(R.string.pick_photo);  //从相册中选择           choices[1] = getString(R.string.take_photo);  //拍照           ListAdapter adapter = new ArrayAdapter<String>(dialogContext,                   android.R.layout.simple_list_item_1, choices);                   AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);           builder.setTitle(R.string.attach_picture_from);           builder.setSingleChoiceItems(adapter, -1,                   new DialogInterface.OnClickListener() {                       public void onClick(DialogInterface dialog, int which) {                           dialog.dismiss();                           switch (which) {                               case 0:                                   HengtuoUtil.log(0, TAG, "选择相册选择");                                 doPickPhotoFromGallery();// 从相册中去获取                                   break;                               case 1:{                                   String status=Environment.getExternalStorageState();                                   if(status.equals(Environment.MEDIA_MOUNTED)){//判断是否有SD卡                                     PHOTO_DIR = new File(Constants.PHOTO_DIR_SDK);                                         HengtuoUtil.log(0, TAG, "选择相机拍摄");                                     doTakePhoto();// 用户点击了从照相机获取                                   }                                   else{                                       //PicPostActivity.showToast("没有SD卡");                                      //无SD卡则直接选择手机                                     PHOTO_DIR = new File(Constants.PHOTO_DIR_PHONE);                                     HengtuoUtil.log(3, TAG, "没有SD卡");                                 }                                   break;                                                             }                         }                     }                 });           builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {                     public void onClick(DialogInterface dialog, int which) {                   dialog.dismiss();               }                         });           AlertDialog dialog = builder.create();         dialog.show();       }     /**      * 拍照获取图片      */      protected void doTakePhoto() {           try {               // Launch camera to take photo for selected contact               PHOTO_DIR.mkdirs();// 创建照片的存储目录               mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());// 给新照的照片文件命名               final Intent intent = getTakePickIntent(mCurrentPhotoFile);               startActivityForResult(intent, CAMERA_WITH_DATA);           } catch (ActivityNotFoundException e) {               Toast.makeText(this, "创建照片失败",Toast.LENGTH_LONG).show();             HengtuoUtil.log(3, TAG, "创建照片失败");         }       }               public static Intent getTakePickIntent(File f) {           Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);           intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));           HengtuoUtil.log(1, "hengtuo.PicPostActivity", "拍照成功");         return intent;       }               /**      * 用当前时间给取得的图片命名      */      private String getPhotoFileName() {           Date date = new Date(System.currentTimeMillis());           SimpleDateFormat dateFormat = new SimpleDateFormat(                   "'IMG'_yyyyMMdd_HHmmss");           return dateFormat.format(date) + ".jpg";       }   /**      *  请求Gallery程序        */    protected void doPickPhotoFromGallery() {           try {               // Launch picker to choose photo for selected contact               final Intent intent = getPhotoPickIntent();               startActivityForResult(intent, PHOTO_CUT_WITH_DATA);           } catch (ActivityNotFoundException e) {               Toast.makeText(this, getString(R.string.photoPickerNotFoundText), Toast.LENGTH_LONG).show();             HengtuoUtil.log(3, TAG, getString(R.string.photoPickerNotFoundText));         }       }               /**      *  封装请求Gallery的intent        * @return      */    public static Intent getPhotoPickIntent() {           Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);           intent.setType("image/*");           intent.putExtra("crop", "true");           intent.putExtra("aspectX", 1);           intent.putExtra("aspectY", 1);           intent.putExtra("outputX", 600);           intent.putExtra("outputY", 400);           intent.putExtra("return-data", true);           return intent;       }               /**      *  因为调用了Camera和Gally所以要判断他们各自的返回情况,他们启动时是这样的startActivityForResult        */    protected void onActivityResult(int requestCode, int resultCode, Intent data) {          if(resultCode == RESULT_CANCELED)//回到选择照片来源         {             doPickPhotoAction();             return;         }         if (resultCode != RESULT_OK){ //回到主页             startActivity(new Intent(PicPostActivity.this,MainActivity.class));             finish();             return;           }         switch (requestCode) {               case PHOTO_PICKED_WITH_DATA: {// 调用Gallery返回的                 break;             }             case CAMERA_WITH_DATA: {// 照相机程序返回的,再次调用图片剪辑程序去修剪图片                  doCropPhoto(mCurrentPhotoFile);                 break;             }             case PHOTO_CUT_WITH_DATA:{                 if (data != null) {                     try {                         setPicToView(data); //裁剪图片                     } catch (Exception e) {                         Toast.makeText(getApplicationContext(), "裁剪失败!", Toast.LENGTH_LONG).show();                         HengtuoUtil.log(3, TAG+".onActivityResult",  "裁剪失败!");                     }                  }                 break;             }         }                   }               private void setPicToView(Intent picdata) throws Exception {         Bundle extras = picdata.getExtras();         if (extras != null) {             Bitmap photo = extras.getParcelable("data");             picName = getPhotoFileName();             File f = new File(Constants.PHOTO_DIR_TEMP);             if(!f.exists())                 f.mkdirs();             mCurrentPhotoFile = new File(Constants.PHOTO_DIR_TEMP+"/"+ picName);             mCurrentPhotoFile.createNewFile();             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(mCurrentPhotoFile));             photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);             bos.flush();             bos.close();             HengtuoUtil.log(0, TAG, "图片保存成功");                           ImageView imgView = (ImageView) findViewById(R.id.img_view);// 获取控件             imgView.setImageBitmap(photo);// 填充控件         }     }     protected void doCropPhoto(File f) {           try {               // 启动gallery去剪辑这个照片               final Intent intent = getCropImageIntent(Uri.fromFile(f));               startActivityForResult(intent, PHOTO_CUT_WITH_DATA);           } catch (Exception e) {               Toast.makeText(this, "没有找到照片", Toast.LENGTH_LONG).show();             HengtuoUtil.log(3, TAG, getString(R.string.photoPickerNotFoundText));         }       }               /**       * Constructs an intent for image cropping. 调用图片剪辑程序       */      public static Intent getCropImageIntent(Uri photoUri) {           Intent intent = new Intent("com.android.camera.action.CROP");           intent.setDataAndType(photoUri, "image/*");           intent.putExtra("crop", "true");           intent.putExtra("aspectX", 1);           intent.putExtra("aspectY", 1);           intent.putExtra("outputX", 600);           intent.putExtra("outputY", 400);           intent.putExtra("return-data", true);           return intent;       }
华为这山寨货!!!!!!!!! 我也碰到同样问题了!!!

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    android手机无法与eclipse或电脑
    C/C++洗牌算法源代码
    servlet技术实现用户名唯一的验证
    E-business suite system servic
    ZOJ 3700 Ever Dream 文章中单词
    TortoiseGit和msysGit安装及使用
    asp中有一段javascipt的网页鼠标
    sharepoint 2010 获取用户信息Us
    设计包含max函数的队列
    随机从数组中取出指定的不重复的
    ZOJ 3700 Ever Dream 文章中单词
    TortoiseGit和msysGit安装及使用
    sharepoint 2010 获取用户信息Us
    mysql主从同步延迟方案解决的学习
    生日旅行总结
    中小板生日快乐随感
    送生日快乐桑葚乳酪小蛋糕
    写给女儿的生日快乐
    总分公司财务核算
    恢复使用繁体字可行性研究报告
    Android http post 上传图片
    安卓本地软件修改密码的实现
    jni thread 退出异常 , nati
    error: Error: No resource 
    Android 3.2上的一个大BUG
    Android 视频流远程监控程序
    java 十六进制字符串转换问题
    QT如何实现左右滑动的按钮
    case expressions must be c
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |