2014-12-20 3 views
0

내 앱에서 사용자는 갤러리 또는 카메라에서 이미지를 laod 할 수 있으며안드로이드에서 이미지보기의 내용 가져 오기

imageView로 표시 할 수 있습니다. 내가하고 싶은 것은 서버에 이미지를 보내는 것입니다. 이를 위해서는

Base64 유형으로 변환해야합니다.

모든 것이 잘하지만 난 그것을 변환하는 이미지 뷰의 내용을 얻는 방법을 모르는,

정확히 코드를 삽입하는 방법.

내 활동은 다음과 같습니다 방법 doInBackground을

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); 
byte [] byte_arr = stream.toByteArray(); 
String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT); 

을하고 난 이 문장을 추가해야합니다 :

base64로 이미지를 변환하는 코드가

public class MainActivity extends Activity {  
    private static int FROM_CAMERA = 1; 
    private static int FROM_GALLERY = 2; 

    ProgressDialog pDialog; 

    JSONParser jsonParser = new JSONParser(); 
    EditText inputName; 
    EditText inputEmail; 
    Button sendData; 
    JSONObject json; 
    ImageView userImg; 


    private static String url_create_user = "http://localhost/android_connect/create_user.php"; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     inputName = (EditText)findViewById(R.id.userName); 
     inputEmail = (EditText)findViewById(R.id.email); 
     sendData = (Button)findViewById(R.id.send); 
     userImg = (ImageView)findViewById(R.id.userImage); 


     sendData.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (isNetworkConnected()) 
        new CreateNewUser().execute(); 
       else 
        Toast.makeText(getApplicationContext(), "Please connect to internet!", Toast.LENGTH_LONG).show(); 

      } 
     }); 


     userImg.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       String[] options = new String[]{"Camera","Gallery"}; 
       AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

       builder.setItems(options, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         if(which == 0){ 
          Intent c = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
          startActivityForResult(c, FROM_CAMERA); 
         } 
         else{ 
          Intent g = new Intent(Intent.ACTION_GET_CONTENT); 
          g.setType("image/*"); 
          startActivityForResult(g, FROM_GALLERY); 

         } 
        } 
       }); 

       builder.create(); 
       builder.show(); 
      } 

     }); 



    } 



    class CreateNewUser extends AsyncTask<String, String,String> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Creating user..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 

     } 


     @Override 
     protected String doInBackground(String... arg0) { 
      String username = inputName.getText().toString(); 
      String email = inputEmail.getText().toString(); 


      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 

      params.add(new BasicNameValuePair("username", username)); 
      params.add(new BasicNameValuePair("email", email)); 

      json = jsonParser.makeHttpRequest(url_create_user, "POST", params); 

      try { 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 


       } else { 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 





     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once done 
      pDialog.dismiss(); 
      if(json != null){ 
       Toast.makeText(getApplicationContext(), "The data has been sent successfully!", Toast.LENGTH_LONG).show(); 
      } 
      else 
       Toast.makeText(getApplicationContext(), "Failed to send data!", Toast.LENGTH_LONG).show(); 


     } 


    } 



    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     ImageView imageView = (ImageView)findViewById(R.id.userImage); 

     if (requestCode == FROM_GALLERY && 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]); 
      String picturePath = cursor.getString(columnIndex); 
      cursor.close(); 

      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
     } 
     else{ 
      if(requestCode == FROM_CAMERA && resultCode == RESULT_OK && null != data) 
      { 

       Bundle extras = data.getExtras(); 
       Bitmap photo = extras.getParcelable("data"); 
       imageView.setImageBitmap(photo); 


      } 
     } 




    } 



    private boolean isNetworkConnected() { 
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo ni = cm.getActiveNetworkInfo(); 
     if (ni == null) return false; 
     else return true; 
    } 

} 

params.add(new BasicNameValuePair("image", image_str)); 

감사합니다 !!! 당신이 base64로 변환 한 후

Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); 

: 당신이 당신의 이미지 뷰의 비트 맵을 얼마나

답변

0

이입니다.

+0

감사합니다. 위치는 어떨까요? – Josef

+0

위치가 무엇을 의미합니까? – kelvincer

+0

변환 코드를 넣을 위치는 어디입니까? – Josef

0

내 문제가 해결되었습니다.

내가이 할 비트 맵 이미지의 내용을 얻으려면 -

Bitmap bitmap = ((BitmapDrawable)userImg.getBackground()).getBitmap(); 

을이 오류없이 잘 작동합니다 여기에 솔루션입니다.

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
if (requestCode == FROM_GALLERY && resultCode == RESULT_OK && null != data) { 

    //Getting the image from gallery and set it to imageView 

} 
else{ 
    if(requestCode == FROM_CAMERA && resultCode == RESULT_OK && null != data) 
    { 

     //Getting the image from camera and set it to imageView 

    } 
} 

Bitmap bitmap = ((BitmapDrawable)userImg.getBackground()).getBitmap(); 

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte [] byte_arr = stream.toByteArray(); 
image = Base64.encodeToString(byte_arr, Base64.DEFAULT); 

}

모든 것이 잘 얻을 :

나는 onActivityResult를 같은()의 변환 코드를 삽입.