2017-01-12 3 views
0

私はこのコードを使用して画像を圧縮しています。バックエンドの画像が壊れています。画像をアップロードするためのカメラとギャラリーの選択オプションがあります。アンドロイドの画像圧縮で働いていて、すべて正常に動作していますが、サーバーに画像をアップロードしたときにその画像が得られました。

public String compressImage(String filePath) { 

    Bitmap scaledBitmap = null; 

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    Bitmap bmp = BitmapFactory.decodeFile(filePath, options); 

    int actualHeight = options.outHeight; 
    int actualWidth = options.outWidth; 
    float maxHeight = 1200.0f; 
    float maxWidth = 600.0f; 
    float imgRatio = actualWidth/actualHeight; 
    float maxRatio = maxWidth/maxHeight; 

    if (actualHeight > maxHeight || actualWidth > maxWidth) { 
    if (imgRatio < maxRatio) { 
    imgRatio = maxHeight/actualHeight; 
    actualWidth = (int) (imgRatio * actualWidth); 
    actualHeight = (int) maxHeight; 
    } else if (imgRatio > maxRatio) { 
    imgRatio = maxWidth/actualWidth; 
    actualHeight = (int) (imgRatio * actualHeight); 
    actualWidth = (int) maxWidth; 
    } else { 
    actualHeight = (int) maxHeight; 
    actualWidth = (int) maxWidth; 
    } 
    } 
    ImageLoadingUtils utils = new ImageLoadingUtils(getApplicationContext()); 
    options.inSampleSize = utils.calculateInSampleSize(options, actualWidth, actualHeight); 
    options.inJustDecodeBounds = false; 
    options.inDither = false; 
    options.inPurgeable = true; 
    options.inInputShareable = true; 
    options.inTempStorage = new byte[16 * 1024]; 

    try { 
    bmp = BitmapFactory.decodeFile(filePath, options); 
    } catch (OutOfMemoryError exception) { 
    exception.printStackTrace(); 

    } 
    try { 
    scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888); 
    } catch (OutOfMemoryError exception) { 
    exception.printStackTrace(); 
    } 

    float ratioX = actualWidth/(float) options.outWidth; 
    float ratioY = actualHeight/(float) options.outHeight; 
    float middleX = actualWidth/2.0f; 
    float middleY = actualHeight/2.0f; 

    Matrix scaleMatrix = new Matrix(); 
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); 

    Canvas canvas = new Canvas(scaledBitmap); 
    canvas.setMatrix(scaleMatrix); 
    canvas.drawBitmap(bmp, middleX - bmp.getWidth()/2, middleY - bmp.getHeight()/2, new Paint(Paint.FILTER_BITMAP_FLAG)); 


    ExifInterface exif; 
    try { 
    exif = new ExifInterface(filePath); 

    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); 
    Log.d("EXIF", "Exif: " + orientation); 
    Matrix matrix = new Matrix(); 
    if (orientation == 6) { 
    matrix.postRotate(90); 
    Log.d("EXIF", "Exif: " + orientation); 
    } else if (orientation == 3) { 
    matrix.postRotate(180); 
    Log.d("EXIF", "Exif: " + orientation); 
    } else if (orientation == 8) { 
    matrix.postRotate(270); 
    Log.d("EXIF", "Exif: " + orientation); 
    } 
    scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
    FileOutputStream out = null; 
    String filename = getFilename(); 
    try { 
    out = new FileOutputStream(filename); 
    scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); 
    } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
    } 
    return filename; 
    } 


    public String getFilename() { 
    File file = new File(Environment.getExternalStorageDirectory().getPath()); 
    if (!file.exists()) { 
    file.mkdirs(); 
    } 
    String uriSting = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg"); 
    return uriSting; 
    } 

    Async task============================================================ 
    private class SellAsync extends AsyncTask<String, String, String> 
    { 
    final ProgressDialog progressDialog = new ProgressDialog(Sell_Main.this); 
    long totalSize = 0; 


    @Override 
    protected String doInBackground(String... urls) 
    { 

    String msg = ""; 
    try 
    { 
    for (int i = 0; i < imageList.size(); i++) 
    { 
    String filePath = compressImage(imageList.get(i)); 

    File f = new File(filePath); 

    new FileUploader().uploadFile(Sell_Main.this, f); 
    } 
    if (!FileUploader.flag) { 
    throw new Exception("File uploading failed"); 
    } 

    ArrayList<NameValuePair> arr = new ArrayList<NameValuePair>(); 
    arr.add(new BasicNameValuePair("brand", brand)); 
    arr.add(new BasicNameValuePair("model", model)); 
    arr.add(new BasicNameValuePair("variant", variant)); 
    arr.add(new BasicNameValuePair("fuel_type", fuelspinner)); 
    arr.add(new BasicNameValuePair("kilometres", km)); 
    arr.add(new BasicNameValuePair("transmission", trans 
     arr.add(new BasicNameValuePair("make_year", makeyear)); 
    arr.add(new BasicNameValuePair("Expected_price", price)); 
    arr.add(new BasicNameValuePair("ownership", owner)); 
    arr.add(new BasicNameValuePair("location", location)); 
    arr.add(new BasicNameValuePair("cid", sPref.getString("id", ""))); 

    String imagesList = ""; 
    for (int i = 0; i < imageList.size(); i++) { 
    File f = new File(imageList.get(i)); 
    imagesList += f.getName() + ","; 
    } 
    arr.add(new BasicNameValuePair("images", imagesList.substring(0, imagesList.length() - 1))); 

    HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost(urls[0]); 

    post.setEntity(new UrlEncodedFormEntity(arr)); 

    HttpResponse response = client.execute(post); 
    msg = EntityUtils.toString(response.getEntity()); 

    } catch (Exception e) { 
    msg = e.getMessage(); 
    } 
    return msg; 
    } 
+0

次のようなコードのこの部分でout.close();を試してみてください、あなたのFileUploaderは、他のファイルで動作しますか? –

+0

はい、私は画像圧縮を使用していない場合、それは完全に正常に動作します –

答えて

0

ちょうどこの

try { 
out = new FileOutputStream(filename); 
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); 
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} finally { 
    out.close(); 
} 
+0

圧縮後に何も起こらない.close –

関連する問題