1

所以(左右),我的要求是:相機意向的照片上傳

  1. 顯示4照片的佔位
  2. 點擊一個佔位符拍照
  3. 顯示4個縮略圖拍攝的照片中片段
  4. 上傳4張照片(一個唯一的HTTP請求時,每張照片250KB最大大小,總1 MB)

我最初認爲這是一個支架阿德/相當容易的任務,但我終於改變了我的想法。我繼續這樣:

  1. 當一個佔位符用戶點擊:

    file = createImageFile(); 
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); 
    startActivityForResult(takePictureIntent); 
    
    private File createImageFile() throws IOException { 
    
        // fileName is just a unique string... 
    
        File storageDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
    
        File file = File.createTempFile(fileName, ".jpg", storageDir); 
    
        return file; 
    } 
    
  2. Google Docs 我設置佔位符縮略圖:

    private void setThumbnail(File file, ImageView target) { 
        int targetWidth = target.getWidth(); 
        int targetHeight = target.getHeight(); 
    
        BitmapFactory.Options options = new BitmapFactory.Options(); 
        options.inJustDecodeBounds = true; 
        BitmapFactory.decodeFile(file.getAbsolutePath(), options); 
        int thumbWidth = options.outWidth; 
        int thumbHeight = options.outHeight; 
    
        int scaleFactor = Math.min(thumbWidth/targetWidth, thumbHeight 
          /targetHeight); 
    
        options.inJustDecodeBounds = false; 
        options.inSampleSize = scaleFactor; 
        options.inPurgeable = true; 
    
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), 
         options); 
    
        target.setImageBitmap(bitmap); 
    } 
    
  3. 服務器我的PHP API需要4個JPEG文件,每個文件最大爲250kb。 我正在使用Loopj庫(1.4.6)來上傳它們。 循環J組請求參數白衣put方法,我們可以使用三種 方式上傳文件:

    params.put("picture", file); // Upload a File 
    params.put("picture", inputStream); // Upload an InputStream 
    params.put("picture", new ByteArrayInputStream(bytes)); // Upload some bytes 
    

從相機意圖保存的圖片太大,無法保留到應用程序和 我的記憶不能將它們「加載」到RAM中(否則我會得到一個OOM異常)。

我應該將它們縮小(或壓縮不知道)迭代到250kb(或某些特定像素大小)和 ,然後覆蓋保存的文件?你能否建議一些東西指向正確的方向?

重要提示:如果可能的話,我想避免重寫相機應用程序,我只是爲了意圖,我沒有足夠的時間。

回答

0

你有沒有嘗試在manifest.xml中添加代碼:

<application 
     android:largeHeap="true"> 

,可讓您以您的應用程序分配更多的內存來妥善管理巨幅照片。

+0

由於兼容性和支持有限,我寧願避免使用此選項。相反,我想減少內存使用量,處理較小的照片。無論如何感謝您的建議。 – Jumpa 2015-02-09 14:17:20