2015-10-23 14 views
5

** Ich kann Bild in remote Mysql-Datenbank mit diesem Code speichern.Aber ich kann nicht die volle Bildqualität von diesem Code zu bekommen.Bilder waren in wirklich schlechter Qualität.Kann man einige vorschlagen eine Lösung dafür?kann nicht volle Bildqualität von Base64 String

Vielen Dank im Voraus. **

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); 
     } 

    } 

Antwort

2

Try unten Code:

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); 
    } 

Hoffe, es wird Ihnen helfen.

+0

fileUri.getPath(); zeigt Fehler. Ich meine Syntaxfehler. – HRCJ

+0

ist "imgPath" eine Zeichenfolge? und sollte es eine globale Variable sein? – HRCJ

+0

Vielen Dank. Es funktionierte für mich – HRCJ

2

statt mit

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

Versuchen Sie es mit

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