2012-05-19 5 views
0

가능한 중복을 이미지를 업로드 :
Calling camera from an activity, capturing an image and uploading to a server안드로이드 활동은 카메라를 열고 서버에

다음

이 내가 인터넷에서 얻은 코드 :

package com.android.imageuploader; 

import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 

import com.android.imageuploader.R; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 

public class ImageUploaderActivity extends Activity { 
    private static final int REQUEST_CODE = 1; 
    private Button button_1; 
    public int TAKE_PICTURE = 1; 
    private ImageView image_view; 
    private Bitmap bitmap; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     image_view = (ImageView) findViewById(R.id.result); 
     button_1 = (Button) findViewById(R.id.button1); 

     button_1.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View arg0) { 

       Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
       startActivityForResult(intent, TAKE_PICTURE); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) 
      try { 
       // We need to recyle unused bitmaps 
       if (bitmap != null) { 
        bitmap.recycle(); 
       } 
       InputStream stream = getContentResolver().openInputStream(
         data.getData()); 
       bitmap = BitmapFactory.decodeStream(stream); 
       stream.close(); 
       image_view.setImageBitmap(bitmap); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     super.onActivityResult(requestCode, resultCode, data); 
    } 
} 

이 버튼과 imageView (기본 이미지가 처음에 들어 있습니다)를 보여줍니다. 버튼을 클릭하면 갤러리로 연결됩니다. y와 내가 imageView에 주어진 이미지 중 하나를 클릭 할 때. 여기 두 가지 질문이 있습니다 1.how 버튼이 카메라에 데려다 할 때 내가 그것을 직접 웹 서버에 이미지를 2.upload을 캡처를

XML :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="pickImage" 
     android:text="Button" > 
    </Button> 

    <ImageView 
     android:id="@+id/result" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:src="@drawable/wic_logo_small" > 
    </ImageView> 

</LinearLayout> 

답변

3

때 사용자가 의도를 통해 카메라를 여는 데 필요한 버튼을 클릭합니다.

public int TAKE_PICTURE =1 
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
    startActivityForResult(intent, TAKE_PICTURE); 

그리고 활성화 된 결과로 카메라에서 캡처 한 이미지를 얻을 수 있습니다.

이제 버튼을 사용하여 코드를 클릭 할 수있는 다음과 같은 URL을 이해 didnt는

Android post Base64 String to PHP

+0

1 일 ... 통과

PLZ 서버에 해당 이미지를 업로드해야? ?? ... 여기 코드에서 나는 어떤 단추 (이것은 응용 프로그램의 유일한 클래스입니다)를 찾을 수 없지만 xml 파일에있는 버튼을 클릭하면 갤러리로 이동합니다. – Housefly

+0

편집해야합니다. 그 XML을 추가하고 하나의 버튼을 추가 & 위의 코드를 작성하는 클릭 리스너에 설정합니다. – Sumant

+0

아니 ... 내 말은 내 질문에 붙여 넣은 코드가 내가 설명한 방법대로 완벽하게 작동한다는 것입니다. 그러나 전체 코드에는 단추가 없습니다 ... 어떻게 그것을 클릭하고 이동할 수 있습니까? 갤러리??? – Housefly

관련 문제