2017-01-30 6 views
0

画像をキャプチャしてサーバーにアップロードするアン​​ドロイドアプリを作成したいのですが、コードが正常に動作しています。つまり、画像をキャプチャしてhttp postメソッドを使用してサーバーに送信できます。アンドロイドキャプチャして低品質の画像しかアップロードしない

しかし、問題は...私のサーバーでは、画像は低品質でしか得られません。実際の画像はほぼ500-900Kbです。しかし、アップロードされたサイズはわずか5-10kbです

- アプリ内のプレビューも低画質を示しています。私は圧縮またはサイズ変更したくありません。私は

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.btnCamera: 

      Intent cameraintent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraintent, 101); 
      break; 
     case R.id.btnSubmit: 
      if (cd.isConnectingToInternet()) { 
       if (!upflag) { 
        Toast.makeText(ImageActivity.this, "Image Not Captured..!", Toast.LENGTH_LONG).show(); 
       } else { 
        saveFile(bitmapRotate, file); 
       } 
      } else { 
       Toast.makeText(ImageActivity.this, "No Internet Connection !", Toast.LENGTH_LONG).show(); 
      } 
      break; 
    } 
} 

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

    try { 
     switch (requestCode) { 
      case 101: 
       if (resultCode == Activity.RESULT_OK) { 
        if (data != null) { 
         selectedImage = data.getData(); // the uri of the image taken 
         if (String.valueOf((Bitmap) data.getExtras().get("data")).equals("null")) { 
          bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage); 
         } else { 
          bitmap = (Bitmap) data.getExtras().get("data"); 
         } 
         if (Float.valueOf(getImageOrientation()) >= 0) { 
          bitmapRotate = rotateImage(bitmap, Float.valueOf(getImageOrientation())); 
         } else { 
          bitmapRotate = bitmap; 
          bitmap.recycle(); 
         } 

         ivImage.setVisibility(View.VISIBLE); 
         ivImage.setImageBitmap(bitmapRotate); 

// Saving image to mobile internal memory for sometime 
         String root = getApplicationContext().getFilesDir().toString(); 
         File myDir = new File(root + "/androidlift"); 
         myDir.mkdirs(); 

         Random generator = new Random(); 
         int n = 10000; 
         n = generator.nextInt(n); 

//Give the file name that u want 
         fname = "sg" + n + ".jpg"; 

         imagepath = root + "/androidlift/" + fname; 
         file = new File(myDir, fname); 
         upflag = true; 
        } 
       } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

public static Bitmap rotateImage(Bitmap source, float angle) { 
    Bitmap retVal; 

    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 
    retVal = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); 

    return retVal; 
} 

// In some mobiles image will get rotate so to correting that this code will help us 
private int getImageOrientation() { 
    final String[] imageColumns = {MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION}; 
    final String imageOrderBy = MediaStore.Images.Media._ID + " DESC"; 
    Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,imageColumns, null, null, imageOrderBy); 

    if (cursor.moveToFirst()) { 
     int orientation = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION)); 
     System.out.println("orientation===" + orientation); 
     cursor.close(); 
     return orientation; 
    } else { 
     return 0; 
    } 
} 

// Saving file to the mobile internal memory 
private void saveFile(Bitmap sourceUri, File destination) { 
    if (destination.exists()) destination.delete(); 
    try { 

     FileOutputStream out = new FileOutputStream(destination); 
     sourceUri.compress(Bitmap.CompressFormat.JPEG, 100, out); 
     out.flush(); 
     out.close(); 
     if (cd.isConnectingToInternet()) { 
      new DoFileUpload().execute(); 
     } else { 
      Toast.makeText(ImageActivity.this, "No Internet Connection..", Toast.LENGTH_LONG).show(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

class DoFileUpload extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 

     pDialog = new ProgressDialog(ImageActivity.this); 
     pDialog.setMessage("wait uploading Image.."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

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

     try { 
      // Set your file path here 
      FileInputStream fstrm = new FileInputStream(imagepath); 
      // Set your server page url (and the file title/description) 
      HttpFileUpload hfu = new HttpFileUpload("http://www.example.com/test/save.php", "ftitle", "fdescription", fname); 
      upflag = hfu.Send_Now(fstrm); 
     } catch (FileNotFoundException e) { 
      // Error: File not found 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String file_url) { 
     if (pDialog.isShowing()) { 
      pDialog.dismiss(); 
     } 
     if (upflag) { 
      Toast.makeText(getApplicationContext(), "Uploading Complete", Toast.LENGTH_LONG).show(); 

はまた何の画像圧縮がない私のサーバー上の実際のサイズの画像を必要とするか、キーの下に余分に私のPHPスクリプトで

+0

あなたはこの問題を報告しています。問題は毎週少なくとも2〜3回報告されます。だから小さなグーグルがこれらの投稿をすべてあなたに与えてくれたでしょう。あなたの開始の意図は間違っています。なぜなら、画像を保存する場所を示す追加の出力パラメータを与えるべきであるからです。その後、指定されたファイルパスを使用して、ビットマップの代わりにファイルをアップロードすることができます。 – greenapps

+0

ええ、私はそれを得た。 –

答えて

関連する問題