2015-10-23 49 views
5

** 我可以在遠程MySQL數據庫使用該code.But保存圖片,我不能得到充分的圖像質量這個code.Images是非常糟糕quality.Can有人建議它的解決方案?不能得到的base64字符串完整圖像質量

在此先感謝。 **

public void onActivityResult(int reqCode, int resCode, Intent data) { 

     if (resCode == RESULT_OK) { 
      if (reqCode == 1) { 
       Bitmap photo = null; 
       imageURI = data.getData(); 
       try { 



     photo = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageURI); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       Image.setImageBitmap(photo); 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       photo.compress(Bitmap.CompressFormat.JPEG, 90, stream); //compress to which format you want. 
       byte[] byte_arr = stream.toByteArray(); 
       String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT); 
       try { 
        image_str1 = URLEncoder.encode(image_str, "UTF-8"); 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } 

      } 

     } 
     if (reqCode == CAMERA_REQUEST && resCode == RESULT_OK) { 

      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      Image.setImageBitmap(photo); 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      photo.compress(Bitmap.CompressFormat.JPEG, 90, stream); //compress to which format you want. 
      byte[] byte_arr = stream.toByteArray(); 
      String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT); 
      try { 
       image_str1 = URLEncoder.encode(image_str, "UTF-8"); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 
      // Photo=getBytes(photo); 
     } 

    } 

回答

2

試試下面的代碼:

if (requestCode == SELECT_FILE && resultCode == RESULT_OK 
        && null != data) { 


       Uri selectedImage = data.getData(); 
       String[] filePathColumn = { MediaStore.Images.Media.DATA }; 


       Cursor cursor = getContentResolver().query(selectedImage, 
         filePathColumn, null, null, null); 

       cursor.moveToFirst(); 

       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       imgPath = cursor.getString(columnIndex); 
       cursor.close(); 
}else if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) { 

       imgPath = fileUri.getPath(); 
       if (imgPath != null && !imgPath.isEmpty()) {        

       encodeImagetoString(); 

       } else { 
       Toast.makeText(getApplicationContext(),          getResources().getString(R.string.some_error_occured), 
            Toast.LENGTH_LONG).show(); 
         } 
} 

encodeImagetoString()

public void encodeImagetoString() { 
     new AsyncTask<Void, Void, String>() { 

      protected void onPreExecute() { 

      }; 

      @Override 
      protected String doInBackground(Void... params) { 
       BitmapFactory.Options options = null; 
       options = new BitmapFactory.Options(); 
       options.inSampleSize = 3; 
       bitmap = BitmapFactory.decodeFile(imgPath, options); 

       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

       bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream); 
       byte[] byte_arr = stream.toByteArray(); 

       encodedString = Base64.encodeToString(byte_arr, 0); 
       return ""; 
      } 

      @Override 
      protected void onPostExecute(String msg) { 
       Log.e("Success" , "Success"); 
      } 

     }.execute(null, null, null); 
    } 

希望它會幫助你。

+0

fileUri.getPath()

photo.compress(Bitmap.CompressFormat.JPEG, 90, stream); 

嘗試;顯示錯誤。我的意思是語法錯誤。 – HRCJ

+0

是「imgPath」的一個字符串?它應該是一個全球變量? – HRCJ

+0

非常感謝。我爲我工作 – HRCJ

2

,而不是使用使用

photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);