我在我的应用程序中使用相机,通过在我的应用程序本身中调用相机api,(我不打算打开Android的主要相机应用程序)。
我在相机预览开启时在屏幕上显示叠加图像,该叠加图像根据方向传感器的变化进行动画处理。
我的问题是,当我捕获图像时,图像被捕获并显示在屏幕上,但它没有保存在外部目录中,我已尽力了,但我没有成功, PLZ。帮助我如何将图像保存到外部目录,你的帮助应该非常感谢。 提前致谢。我发布了我的整个代码,plz帮助我实现什么以及在哪里实施.....
package com.example.compassapp;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener {
// define the display assembly compass picture
private ImageView image;
// record the compass picture angle turned
private float currentDegree = 0f;
// device sensor manager
private SensorManager mSensorManager;
TextView tvHeading;
Canvas canvas;
FrameLayout preview;
ImageButton button_capture;
private Camera cameraObject;
private ShowCamera showCamera;
private ImageView pic;
public static Camera isCameraAvailiable(){
Camera object = null;
try {
object = Camera.open();
}
catch (Exception e){
}
return object;
}
private PictureCallback capturedIt = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
if(bitmap==null){
Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show();
}
cameraObject.release();
}
private void SaveImage(Bitmap finalBitmap){
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView)findViewById(R.id.image);
button_capture=(ImageButton)findViewById(R.id.button_capture);
cameraObject = isCameraAvailiable();
//showCamera = new ShowCamera(this, cameraObject);
showCamera = new ShowCamera(this, cameraObject);
preview = (FrameLayout) findViewById(R.id.preview);
preview.addView(showCamera);
button_capture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
cameraObject.takePicture(null, null, capturedIt);
//cameraObject.takePicture(null, null, capturedIt);
}
});
// our compass image
// TextView that will tell the user what degree is he heading
tvHeading = (TextView) findViewById(R.id.tvHeading);
// initialize your android device sensor capabilities
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
// for the system's orientation sensor registered listeners
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause() {
super.onPause();
// to stop the listener and save battery
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
// get the angle around the z-axis rotated
float degree = Math.round(event.values[0]);
tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
// create a rotation animation (reverse turn degree degrees)
RotateAnimation ra = new RotateAnimation(
currentDegree,
-degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
// how long the animation will take place
ra.setDuration(210);
// set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
image.startAnimation(ra);
currentDegree = -degree;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// not in use
}
public void onBackPressed() {
//super.onBackPressed();
Log.d("back button", "back button pressed");
AlertDialog.Builder ad1=new AlertDialog.Builder(this);
ad1.setMessage("Are you sure you want to exit? ");
ad1.setCancelable(false);
ad1.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
ad1.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
MainActivity.this.finish();
}
});
AlertDialog alert=ad1.create();
alert.show();
}
}
答案 0 :(得分:1)
add this code in onActivityResult. This will store your image in a folder named "FolderName"
String extr = Environment.getExternalStorageDirectory().toString()
+ File.separator + "YourFolderName";
File myPath = new File(extr, FolderName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
bitMap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(context.getContentResolver(),
bitMap, myPath.getPath(), uniqueFileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
还需要在Manifest上设置权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 1 :(得分:0)
您是否已将此权限放入AndroidManifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />