我使用Java制作了自己的Android相机应用程序,一切似乎工作正常,但是当我捕获图像时,它不会显示在我的目录中 - 我 - 我从同一目录中删除一个或多个要删除的文件。你们知道为什么会这样吗?
我通过手机图片库和Windows 10文件管理器(通过USB线)查看了我的目录。
这是我的代码:
CameraDemo.java
package nl.arjenvangaal.takepicturev2;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;
public class CameraDemo extends Activity {
Preview preview;
Button saveButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //load layout file
preview = new Preview(this); //create new camera preview
((FrameLayout) findViewById(R.id.preview)).addView(preview); //display new preview
//button listener
saveButton = (Button)findViewById(R.id.saveButton);
saveButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
}
};
/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
}
};
/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss", Locale.US);
String date = dateFormat.format(new Date());
String photoFile = "Foto_"+ date + ".jpg";
File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); //mapje
String filename =sdDir + File.separator + photoFile; //mapje + / + filenaam
File pictureFile = new File(filename); //uiteindelijke bestandslocatie
FileOutputStream outStream; //me create new output
try {
outStream = new FileOutputStream(pictureFile);
outStream.write(data);
outStream.close();
Toast.makeText(getApplicationContext(), "Foto opgeslagen.", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Foto kon niet worden opgeslagen.", Toast.LENGTH_LONG).show();
}
}
};
}
Preview.java
package nl.arjenvangaal.takepicturev2;
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
class Preview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
Preview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.setDisplayOrientation(90); //Roteer het beeld 90 graden zodat het goed wordt weergegeven
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
Preview.this.invalidate();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
camera.stopPreview();
;
//voorkom error als je het programma afsluit en heropent
if (camera != null) {
camera.release();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
// You need to choose the most appropriate previewSize for your app
Camera.Size previewSize = previewSizes.get(0); // .... select one of previewSizes here
parameters.setPreviewSize(previewSize.width, previewSize.height);
camera.setParameters(parameters);
//voorkom error als je het programma afsluit en heropent
if (mHolder.getSurface() == null) {
return;
}
try {
camera.stopPreview();
} catch (Exception e) {
//You can ignore this, because this means the Preview doesn't Exist
//So, no need to try stopping
}
try {
camera.setPreviewDisplay(mHolder);
camera.startPreview();
} catch (Exception e) {
//Catch this
}
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint p= new Paint(Color.RED);
Log.d(TAG, "draw");
canvas.drawText("PREVIEW", canvas.getWidth() / 2, canvas.getHeight() / 2, p);
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/layout">
<FrameLayout
android:id="@+id/preview"
android:layout_weight="1" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" android:layout_gravity="center"
android:id="@+id/saveButton"
/>
</LinearLayout>