2013-01-24 2 views
1

나는 도움이 필요해. 것은 namecard 독자에 응용을하고 사진을 찍기 위하여 사진기 기능을 이용할 필요가있다. 하지만 내 앱이 앱을 종료하고 갤러리로 이동하지 않고도 저장 한 여러 가지 그림을 볼 수있게하려면 어떻게해야합니까? 아래 내가 사용하는 카메라 소스 코드 감사합니다 ..입니다 내가 카메라카메라 안드로이드에 대한 소스 코드

을 시작하는 버튼을 사용하고 CameraMainActivity.Java

private static final int REQUEST_CODE = 1; 
private static final int CAMERA_PIC_REQUEST = 1337; 
private Bitmap bitmap; 
private ImageView imageView; 
private Button save, capture; 

/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_camera_main); 
    imageView = (ImageView) findViewById(R.id.result); 
    save = (Button) findViewById(R.id.save); 
    save.setOnClickListener(this); 
    capture = (Button) findViewById(R.id.capture); 
    capture.setOnClickListener(this); 
} 

public void captureImage() { 
    // Capture image from camera 

     Intent intent = new Intent(
       android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

     startActivityForResult(intent, CAMERA_PIC_REQUEST); 

} 

public void pickImage() { 
    // To pick a image from file system 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 
    startActivityForResult(intent, REQUEST_CODE); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == REQUEST_CODE) { 
      try { 
       // We need to recycle unused bitmaps 
       if (bitmap != null) { 
        bitmap.recycle(); 
       } 
       InputStream stream = getContentResolver().openInputStream(
         data.getData()); 
       bitmap = BitmapFactory.decodeStream(stream); 
       stream.close(); 
       imageView.setImageBitmap(bitmap); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } else if (requestCode == CAMERA_PIC_REQUEST) { 

       if (bitmap != null) { 
        bitmap.recycle(); 
       } 
       bitmap = (Bitmap) data.getExtras().get("data"); 
       if (data.getExtras().get("data") == null) 
       Toast.makeText(getApplicationContext(), 
         "No image returned", Toast.LENGTH_LONG).show(); 
       else 
       imageView.setImageBitmap(bitmap); 



     } 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_camera_main, menu); 
    return true; 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    if (v == save) { 
     BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); 
     Bitmap bitmap = drawable.getBitmap(); 
     File sdCardDirectory = Environment.getExternalStorageDirectory(); 
     File image = new File(sdCardDirectory, "test.png"); 
     boolean success = false; 

     // Encode the file as a PNG image. 
     FileOutputStream outStream; 
     try { 

      outStream = new FileOutputStream(image); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
      /* 100 to keep full quality of the image */ 

      outStream.flush(); 
      outStream.close(); 
      success = true; 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     if (success) { 
      Toast.makeText(getApplicationContext(), 
        "Image saved with success", Toast.LENGTH_LONG).show(); 
     } else { 
      Toast.makeText(getApplicationContext(), 
        "Error during image saving", Toast.LENGTH_LONG).show(); 
     } 
    } else if (v == capture) { 
     captureImage(); 
    } 

} 

}

`

main.xml

<Button 
     android:id="@+id/takeviewpic" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Take/View Pic" /> 

안드로이드`

+0

응용 프로그램 폴더를 만들어 IO 파일 관리자에 이미지를 저장할 수 있습니다. 그리고 해당 폴더에서 이미지를 가져 와서 갤러리 또는 이미지보기에 표시하십시오. – Maulik

답변

0

는 이미지 뷰 (또는 ImageViews)로 활동을 넣고

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="android.permission.CALL_PHONE"/> 
<uses-permission android:name="android.permission.SEND_SMS"/> 
<uses-permission android:name="android.permission.RECEIVE_SMS"/> 
<uses-permission android:name="android.permission.READ_SMS"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-feature 
    android:name="android.hardware.camera" 
    android:required="false" /> 
<uses-feature 
    android:name="android.hardware.camera.front" 
    android:required="false" /> 
<uses-feature 
    android:name="android.hardware.camera.autofocus" 
    android:required="false" /> 
<uses-feature 
    android:name="android.hardware.camera.flash" 
    android:required="false" /> 
<uses-feature android:name="android.hardware.screen.landscape" /> 
<uses-feature 
    android:name="android.hardware.wifi" 
    android:required="false" /> 
<uses-feature 
    android:name="android.hardware.touchscreen" 
    android:required="false" /> 

<uses-sdk android:minSdkVersion="10" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <uses-library android:name="com.google.android.maps" /> 
    <activity 
     android:name="sp.com.Splash" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".Main" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="main" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".NameCardList" > 
    </activity> 
    <activity android:name=".DetailForm" > 
    </activity> 
    <activity android:name=".EditPreferences" > 
    </activity> 
    <activity android:name=".NameCardMap" > 
    </activity> 
    <activity android:name=".SmsReceiver" > 
    </activity> 
    <activity 
     android:name=".CameraMainActivity" 
     android:label="@string/app_name" 
     android:clearTaskOnLaunch="true" 
     android:configChanges="orientation|keyboardHidden" 
     android:stateNotNeeded="true" 
    /> 
</application> 
매니페스트와 사용자가 만든 파일에의 소스를 설정합니다.

2

내이 카메라 예제는 당신을 도울 수있다 : -

import android.hardware.Camera; 

import android.hardware.Camera.CameraInfo; 

import android.os.Bundle; 

import android.app.Activity; 

import android.content.pm.PackageManager; 

import android.util.Log; 

import android.view.Menu; 

import android.view.View; 

import android.widget.Toast; 


public class Main extends Activity { 

    private final static String DEBUG_TAG = "MakePhotoActivity"; 

    private Camera camera; 

    private int cameraId = 0; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 


if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 

      Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG).show(); 

     } 
else { 

      cameraId = findFrontFacingCamera(); 

      camera = Camera.open(cameraId); 

      if (cameraId < 0) { 

      Toast.makeText(this, "No front facing camera found.", 

       Toast.LENGTH_LONG).show(); 

      } 

     } 

} 

public void onClick(View view) { 

    camera.takePicture(null, null,new PhotoHandler(getApplicationContext())); 

    } 

private int findFrontFacingCamera() 
{ 

int cameraId = -1; 

    // Search for the front facing camera 

    int numberOfCameras = Camera.getNumberOfCameras(); 

    for (int i = 0; i < numberOfCameras; i++) 

{ 

CameraInfo info = new CameraInfo(); 

Camera.getCameraInfo(i, info); 

if (info.facing == CameraInfo.CAMERA_FACING_FRONT) 

{ 
     Log.d(DEBUG_TAG, "Camera found"); 

     cameraId = i; 

    break; 

    } 

    } 

    return cameraId; 

    } 

@Override 

protected void onPause() 
{ 

if (camera != null) 
{ 


    camera.release(); 

    camera = null; 

} 

super.onPause(); 

} 



} 

그리고 사진 핸들러의 다른 클래스 .. !!! : -

import java.io.File; 
import java.io.FileOutputStream; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

import android.content.Context; 
import android.hardware.Camera; 
import android.hardware.Camera.PictureCallback; 
import android.os.Environment; 
import android.provider.SyncStateContract.Constants; 
import android.util.Log; 
import android.widget.Toast; 

    public class PhotoHandler implements PictureCallback { 
private final Context context; 

    public PhotoHandler(Context context) { 
    this.context = context; 
    } 

    public void onPictureTaken(byte[] data, Camera camera) { 

    File pictureFileDir = getDir(); 

    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) { 

     Toast.makeText(context, "Can't create directory to save image.", 
      Toast.LENGTH_LONG).show(); 
     return; 

    } 

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss"); 
    String date = dateFormat.format(new Date()); 
    String photoFile = "Picture_" + date + ".jpg"; 

    String filename = pictureFileDir.getPath() + File.separator + photoFile; 

    File pictureFile = new File(filename); 

    try { 
     FileOutputStream fos = new FileOutputStream(pictureFile); 
     fos.write(data); 
     fos.close(); 
     Toast.makeText(context, "New Image saved:" + photoFile, 
      Toast.LENGTH_LONG).show(); 
    } catch (Exception error) { 

     Toast.makeText(context, "Image could not be saved.", 
      Toast.LENGTH_LONG).show(); 
    } 
    } 

    private File getDir() { 
    File sdDir = Environment 
     .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
    return new File(sdDir, "CameraAPIDemo"); 
    } 

} 

u는 다른 PLZ 의견을 추가 할 무언가를 원하는 경우에

. .. !!!!!

+0

Ruchit Shah -이 예제의 전체 파일을 가지고 있습니까? 나는 내 프로그램에 그것을 넣는 법을 알고있다. 소스 코드를 입력하는 방법을 이해하려면 XML로 전체 파일을 볼 필요가 있습니다. 유감스럽게 생각합니다 : – aiman

+0

[카메라 갤러리] (http://www.technotalkative.com/android-select-multiple-photos-from-gallery/)이 링크는 도움이 될 수 있습니다 ... !!!!! –

관련 문제