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