Android相机应用:输出位图到秒activity
Android Camera Application: Outputting bitmap to second activity
我想将图像保存在内部,然后在下一个Activity中显示。这是相机 activity(预览和一切正常,但是当我按下捕捉按钮时,我被发送到第二个 activity 但它只是一个白色屏幕......没有显示位图图像) .
主摄像头Activity:
public class MainActivity extends FragmentActivity {
private static final String TAG = "CameraActivity";
public static final int MEDIA_TYPE_IMAGE = 1;
private Camera mCamera;
private CameraPreview mPreview;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
if(checkCameraHardware(mContext)){
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
// Add a listener to the Capture button
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
}
}
);
}
}
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(final byte[] data, Camera camera) {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
try {
FileOutputStream fos = openFileOutput("img.jpg", Context.MODE_PRIVATE);
fos.write(data);
fos.close();
return "img.jpg";
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
return null;
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
return null;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result != null){
Intent intent = new Intent(mContext, ImageDisplayActivity.class);
intent.putExtra(ImageDisplayActivity.KEY_PATH, "img.jpg");
startActivity(intent);
}
}
}.execute();
}
};
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
/** Create a File for saving an image or video */
}
这是第二个,显示图片activity:
public class ImageDisplayActivity extends FragmentActivity {
public static final String KEY_PATH = "img.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//final Bundle extras = getIntent().getExtras();
Intent intent = getIntent();
String path = getIntent().getStringExtra("EXTRA_FILE");
try {
java.io.FileInputStream in = this.openFileInput(path);
Bitmap bitmap = BitmapFactory.decodeStream(in);
ImageView mImg;
mImg = (ImageView) findViewById(R.id.image_displayer);
mImg.setRotation(90);
mImg.setImageBitmap(bitmap);
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private Bitmap decodeFile(File f){
Bitmap b = null;
try {
BitmapFactory.Options o = new BitmapFactory.Options();
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
BitmapFactory.Options o2 = new BitmapFactory.Options();
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
}
return b;
}
}
这里是 LogCat:
02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ java.lang.NullPointerException
02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ContextImpl.makeFilename(ContextImpl.java:2456)
02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ContextImpl.openFileInput(ContextImpl.java:1072)
02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.content.ContextWrapper.openFileInput(ContextWrapper.java:180)
02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at com.commonsware.android.test1.ImageDisplayActivity.onCreate(ImageDisplayActivity.java:33)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5372)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.access0(ActivityThread.java:165)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5455)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
使用 ImageDisplayActivity.KEY_PATH
作为 key
而不是 EXTRA_FILE
来获取从之前的 Activity 传递过来的值,其 Intent :
String path = getIntent().getStringExtra(ImageDisplayActivity.KEY_PATH);
我想将图像保存在内部,然后在下一个Activity中显示。这是相机 activity(预览和一切正常,但是当我按下捕捉按钮时,我被发送到第二个 activity 但它只是一个白色屏幕......没有显示位图图像) .
主摄像头Activity:
public class MainActivity extends FragmentActivity {
private static final String TAG = "CameraActivity";
public static final int MEDIA_TYPE_IMAGE = 1;
private Camera mCamera;
private CameraPreview mPreview;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
if(checkCameraHardware(mContext)){
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
// Add a listener to the Capture button
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
}
}
);
}
}
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(final byte[] data, Camera camera) {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
try {
FileOutputStream fos = openFileOutput("img.jpg", Context.MODE_PRIVATE);
fos.write(data);
fos.close();
return "img.jpg";
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
return null;
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
return null;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result != null){
Intent intent = new Intent(mContext, ImageDisplayActivity.class);
intent.putExtra(ImageDisplayActivity.KEY_PATH, "img.jpg");
startActivity(intent);
}
}
}.execute();
}
};
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
/** Create a File for saving an image or video */
}
这是第二个,显示图片activity:
public class ImageDisplayActivity extends FragmentActivity {
public static final String KEY_PATH = "img.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//final Bundle extras = getIntent().getExtras();
Intent intent = getIntent();
String path = getIntent().getStringExtra("EXTRA_FILE");
try {
java.io.FileInputStream in = this.openFileInput(path);
Bitmap bitmap = BitmapFactory.decodeStream(in);
ImageView mImg;
mImg = (ImageView) findViewById(R.id.image_displayer);
mImg.setRotation(90);
mImg.setImageBitmap(bitmap);
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private Bitmap decodeFile(File f){
Bitmap b = null;
try {
BitmapFactory.Options o = new BitmapFactory.Options();
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
BitmapFactory.Options o2 = new BitmapFactory.Options();
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
}
return b;
}
}
这里是 LogCat:
02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ java.lang.NullPointerException
02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ContextImpl.makeFilename(ContextImpl.java:2456)
02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ContextImpl.openFileInput(ContextImpl.java:1072)
02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.content.ContextWrapper.openFileInput(ContextWrapper.java:180)
02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at com.commonsware.android.test1.ImageDisplayActivity.onCreate(ImageDisplayActivity.java:33)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5372)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.access0(ActivityThread.java:165)
02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5455)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
使用 ImageDisplayActivity.KEY_PATH
作为 key
而不是 EXTRA_FILE
来获取从之前的 Activity 传递过来的值,其 Intent :
String path = getIntent().getStringExtra(ImageDisplayActivity.KEY_PATH);