gpt4 book ai didi

android - 用相机拍摄照片并将它们保存在特定文件夹中

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:06 27 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,它允许用户使用 android 相机拍摄一些照片。用户拍摄这些照片是为了参加摄影比赛。所以,他拍了几张照片,这些照片应该保存到具体目的地,过了一会儿,他在这些照片之间循环,并决定他将与哪一张一起参加摄影比赛。好吧,为此,照片应该保存在一个特定的文件夹中,而不是在画廊中以及其他不用于比赛的照片中。目前,我只是将照片保存到SD卡中,我不知道如何将它们保存到某个文件夹中。

我必须说,我已经构建了自己的相机,但在保存图像时仍然不知道如何操作。

这是它的样子:

public class EditPhoto extends Activity implements SurfaceHolder.Callback, OnClickListener {
static final int FOTO_MODE = 0;
private static final String TAG = "CameraTest";
Camera mCamera;
boolean mPreviewRunning = false;
private Context mContext = this;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//doing things
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback(){
public void onPictureTaken(byte[] imageData, Camera c) {

if (imageData != null) {

Intent mIntent = new Intent();

StoreByteImage(mContext, imageData, 50, "ImageName");
mCamera.startPreview();

Bundle b = new Bundle();
b.putByteArray("imageData", imageData);
Intent i = new Intent(mContext, ImageDisplayActivity.class);
i.putExtras(b);
startActivity(i);

setResult(FOTO_MODE, mIntent);
finish();

}
}
};



protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}

protected void onStop() {
Log.e(TAG, "onStop");
super.onStop();
}

public void surfaceCreated(SurfaceHolder holder) {
Log.e(TAG, "surfaceCreated");
mCamera = Camera.open();

}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.e(TAG, "surfaceChanged");
if (mPreviewRunning) {
mCamera.stopPreview();
}

Camera.Parameters p = mCamera.getParameters();
List<Size> sizes = p.getSupportedPictureSizes();

p.setPreviewSize(640, 480);
p.setPictureSize(213,350);

mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}

public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, "surfaceDestroyed");
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
}

private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;

public void onClick(View arg0) {

mCamera.takePicture(null, mPictureCallback, mPictureCallback);

}

public static boolean StoreByteImage(Context mContext, byte[] imageData, int quality, String expName) {

File sdImageMainDirectory = new File("/sdcard");
FileOutputStream fileOutputStream = null;
String nameFile;
try {

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 5;
Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length,options);
fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() +"/image.jpg");
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

myImage.compress(CompressFormat.JPEG, quality, bos);

bos.flush();
bos.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return true;
}



}

如果您能指出正确的方向,我会很感激。谢谢

最佳答案

只需在 sdcard 后添加文件夹名称(即/sdcard/images/wildlife)。为确保指定文件夹存在,调用方法File.mkdirs() .并且请不要使用硬编码字符串/sdcard 访问SDCard,使用Environment.getExternalStorageDirectory方法。返回任何查询。

编辑:

public class ImageViewActivity extends Activity {

private String[] imageDirs;
private Spinner dirSpinner;

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

getDirs();

populateSpinner();

}

private void populateSpinner() {
dirSpinner = (Spinner) findViewById(R.id.dirSpinner);
ArrayAdapter<String> dirAdapter = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_spinner_dropdown_item, imageDirs);

dirAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dirSpinner.setAdapter(dirAdapter);
dirSpinner.setOnItemSelectedListener(new OnImageItemSelectedListener());
}

/**
* Retrieves all the image files in the given directory.
*/
public File[] retrieveContents(String dirPath) {

File parentDir = new File(dirPath);

if (!parentDir.exists()) {
return null;
}

if (!parentDir.isDirectory()) {
return null;
}

File[] fileContents = null;

FilenameFilter filter = new FilenameFilter();
fileContents = parentDir.listFiles(filter);

filter = null;

parentDir = null;

return fileContents;
}

/**
* Inner class to get images only.
*/
private class FilenameFilter implements FileFilter {
@Override
public boolean accept(File dir) {
return dir.getName().toLowerCase()
.endsWith(".jpg;*.bmp;*.png;*.gif");
}
}

/**
* Returns the names of sub-dirs in Images dir.
*/
private void getDirs() {
String parentDir = Environment.getExternalStorageDirectory()
+ "/Images";

File[] imageFolders = new File(parentDir).listFiles();
ArrayList<String> dirList = new ArrayList<String>();

for (int i = 0; i < imageFolders.length; i++) {
if (imageFolders[i].isDirectory()) {
dirList.add(imageFolders[i].getName());
}
}

imageDirs = (String[]) dirList.toArray(new String[0]);
}

public class OnImageItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {

Toast.makeText(
parent.getContext(),
"The dir is " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
}

关于android - 用相机拍摄照片并将它们保存在特定文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6895160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com