当前位置:操作系统 > 安卓/Android >>

选择系统图片方法时返回图片真实路径

Java代码 
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
        if (resultCode == RESULT_OK){ 
            //拍照或者选择图片 
            if (requestCode == RESULT_CAPTURE_IMAGE || requestCode == RESULT_CHOOSE_IMAGE) { 
                try { 
                     
                    Uri uri = data.getData();   
                    String[] proj = { MediaStore.Images.Media.DATA };   
                    Cursor actualimagecursor = 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);   
                    System.out.println("图片真实路径:"+img_path); 
             
//                   
//                  ContentResolver resolver = getContentResolver(); 
//                  Uri uri = data.getData(); 
//                  System.out.println("选择图片路径:" + uri); 
//                  Bitmap img = MediaStore.Images.Media.getBitmap(resolver,uri); 
////                    Bitmap bitmap = BitmapFactory.decodeStream(resolver.openInputStream(uri)); 
////                    Bitmap img= (Bitmap)data.getExtras().get("data");   //获取缩略图 
//                  try { // 压缩图片 
//                      File file = new File(tempPath); 
//                      FileOutputStream outputStream = null; 
//                      outputStream = new FileOutputStream(file); 
// 
//                      if (img.getWidth() > 800 || img.getHeight() > 600) { 
//                          img = Bitmap.createScaledBitmap(img, 800, 600, false); 
//                      } 
//                      img.compress(Bitmap.CompressFormat.JPEG, 90, outputStream); 
//                      outputStream.flush(); 
//                      outputStream.close(); 
//                       
//                      sendFile(file,uri.toString());  //发送图片 
////                        sendFile(tempPath); //发送图片 
//                       
//                  } catch (IOException e) { 
//                      e.printStackTrace(); 
//                  } 
// 
//                  img = null; 
//                  System.gc(); 
                } catch (OutOfMemoryError e) { 
                    MyMethods.showMsg("获取图出错,请重新操作",this); 
                    e.printStackTrace(); 
                } catch (Exception e) { 
                    e.printStackTrace(); 
                }  
            } 
        } 
    } 

作者“小猪的博客”
 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,