2014-01-30 62 views
0

我想減少從相機和畫廊(1 + MB到150K兩個)拍攝的照片的大小。調整從+ 1mb調整圖像從相機意圖<= 150k Android

  1. 代碼拍照:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    
    //intent.putExtra("crop", "true"); 
    
    intent.putExtra("outputX", 50); 
    
    intent.putExtra("outputY", 50); 
    
    intent.putExtra("aspectX", 1); 
    
    intent.putExtra("aspectY", 1); 
    
    intent.putExtra("outputX", 128); 
    
    intent.putExtra("outputY", 128); 
    
    intent.putExtra("scaleUpIfNeeded", true); 
    
    intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString()); 
    
    intent.putExtra("return-data", false); 
    
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination)); 
    
    startActivityForResult(intent, PICTURE_RESULT); 
    
  2. 意圖從庫中選擇

    Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    
    intent.setType("image/*"); 
    
    //intent.putExtra("crop", "true"); 
    
    intent.putExtra("outputX", 50); 
    
    intent.putExtra("outputY", 50); 
    
    intent.putExtra("aspectX", 1); 
    
    intent.putExtra("aspectY", 1); 
    
    intent.putExtra("scale", true); 
    
    intent.putExtra("scaleUpIfNeeded", true); 
    
    intent.putExtra("return-data", true); 
    
    intent.putExtra(MediaStore.EXTRA_OUTPUT,destination); 
    intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString()); 
    
    startActivityForResult(Intent.createChooser(intent, "Choisir Photo"),SELECT_FILE); 
    

我的活動結果如下

@Override 

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

     super.onActivityResult(requestCode, resultCode, data); 

     if (resultCode == PICTURE_RESULT) { 

      if (requestCode == REQUEST_CAMERA) { 

       Editer.PHOTO_FROM=11; 

       File f = new File(Environment.getExternalStorageDirectory() 
         .toString()); 

       for (File temp : f.listFiles()) { 

        if (temp.getName().equals("temp.jpg")) { 

         f = temp; 

         break; 

        } 

       } 

       //Uri.fromFile(createFile()); 

       Constant.filePath=f.getAbsolutePath(); 

       String fname = "user_image_golf.jpg"; 

       BitmapFactory.Options options = new BitmapFactory.Options(); 

       options.inTempStorage = new byte[16 * 1024]; 

       options.inSampleSize = 4; 

       options.outWidth = 100; 

       options.outHeight = 100; 

       String root = Environment.getExternalStorageDirectory().toString(); 

       File myDir = new File(root + "user_image_golf.jpg"); // ==/
       myDir.mkdirs(); 

       destination = new 
File(Environment.getExternalStorageDirectory(),"user_image_golf.jpg"); 


       Bitmap bitmap = BitmapFactory.decodeFile(destination.toString(), options); 

       Bitmap map = Bitmap.createScaledBitmap(bitmap, 100, 100, true); 
       ByteArrayOutputStream bao = new ByteArrayOutputStream(); 

       map.compress(Bitmap.CompressFormat.PNG, 50, bao); 

       //byte[] ba = bao.toByteArray(); 

       File file = new File (myDir, f.getAbsolutePath()); 

       if (file.exists()) file.delete(); 

       try { 

        FileOutputStream out = new FileOutputStream(file); 

        map.compress(Bitmap.CompressFormat.JPEG, 20, out); 

        out.flush(); 

        out.close(); 

       } catch (Exception e) { 

        e.printStackTrace(); 
       } 

       System.out.println("//imagedata=Base64.encodeBytes(ba);"); 

       ///imagedata=Base64.encodeBytes(ba); 

       //rotateImage(Uri.fromFile(destination).toString()); 

      } 


      else if (requestCode == SELECT_FILE) { 

       Uri selectedImageUri = data.getData(); 

       Constant.filePath =selectedImageUri.toString(); 

       Editer.PHOTO_FROM=2; 

       System.out.println("getAbsolutePath() "+selectedImageUri); 

       System.out.println("selectedImageUri-getAbsolutePath() 
"+getPath(selectedImageUri,m)); 


      } 

     } 
     m.finish(); 

    } 

如果我從圖庫中選擇一張照片或從相機拍攝照片,則來自相機的新照片或來自相冊的最新選定圖像應覆蓋保存在SD卡上的照片。

+0

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap –

+0

爲什麼你有一個循環來搜索文件,而不是'F =新文件(f,「temp.jpg」);'直接? – njzk2

+0

nota:PNG在android中是無損的。質量參數被忽略。使用JPG是你需要減小尺寸。 – njzk2

回答

1

試試這個。

private void selectImage() { 
    final CharSequence[] items = { "Take Photo", "Choose from Gallery" }; 

    AlertDialog.Builder builder = new AlertDialog.Builder(BVMobile.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")) { 
       // folder stuff 
       Intent cameraIntent = new Intent(
         android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       File imagesFolder = new File(Environment 
         .getExternalStorageDirectory(), "verificationapp"); 
       imagesFolder.mkdirs(); 

       Date alsoNow = Calendar.getInstance().getTime(); 
       tempfile = new SimpleDateFormat("yyyyMMddHHmmss") 
         .format(alsoNow); 
       File image = new File(imagesFolder, tempfile + ".png"); 
       Uri uriSavedImage = Uri.fromFile(image); 
       cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
         uriSavedImage); 
       startActivityForResult(cameraIntent, REQUEST_CAMERA); 

      } else if (items[item].equals("Choose from Gallery")) { 
       Intent intent = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

       startActivityForResult(intent, SELECT_FILE); 
      } 
     } 
    }); 
    builder.show(); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     if (requestCode == SELECT_FILE) { 
      if (data != null) { 

       Uri selectedImageUri = data.getData(); 

       selectedfile = getRealPathFromURI(selectedImageUri); 

       Date alsoNow = Calendar.getInstance().getTime(); 
       String currentdate = new SimpleDateFormat("yyyyMMddHHmmss") 
         .format(alsoNow); 

       imgname = currentdate + "_" + refid; 

        strPhoto1 = SERVER_FILE_PATH + imgname + ".png"; 




      } 
     } else if (requestCode == REQUEST_CAMERA) { 

      File imagesFolder = new File(
        Environment.getExternalStorageDirectory(), 
        "verificationapp"); 
      File image = new File(imagesFolder, tempfile + ".png"); 
      selectedfile = image.toString(); 

      imgname = tempfile + "_" + refid; 

     } 
    } 
} 

public String converttobase64(String picturePath) { 
    String b64 = null; 
    if (!picturePath.equals("")) { 

     Bitmap bm = BitmapFactory.decodeFile(picturePath); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bm.compress(Bitmap.CompressFormat.JPEG, 50, baos); 

     byte[] b = baos.toByteArray(); 
     b64 = Base64.encodeToString(b, Base64.DEFAULT); 

    } 

    return b64; 
}