我正在处理一个会拍照的应用,然后将其返回并将其放入imageview中。 当它返回缩略图大小的图像时,我能够使它工作。我按照Android开发者页面上的说明创建了一个图像文件但由于某种原因它不适合我。我有一个按钮,启动一个方法,在启动intent之前调用createImageFile()。但是这种方法失败了,相机甚至从未启动过。
这是我的版本:
private File createImageFile() throws IOException
{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").
format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
Log.i("storage: ",storageDir.getAbsolutePath());
File image = File.createTempFile(imageFileName,
".jpg",storageDir);
Log.i("filename: ", "afterimage");
FILENAME = "file:" + image.getAbsolutePath();
Log.i("filename: ", FILENAME);
return image;
}
但是我的代码永远不会超过File image = File.createTempFile(imageFileName, " .JPG",storageDir) 因为我从未见过以下Log.i();
我尝试使用context.getCacheDir()创建storageDir并且相机实际启动但是在拍照后我无法点击复选标记并返回主活动。
如有必要,以下是活动的其余部分:
public class Camera extends Activity
{
private final int PICTURE_ACTIVITY_CODE = 1;
String FILENAME;
File f;
Button b;
ImageView image;
@Override
/**
* onCreate method. Instantiates buttons and textfield
* and sets a listener for button that launches launchTakePhoto
* method.
*/
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
image = (ImageView) findViewById(R.id.img);
b = (Button) findViewById(R.id.b1);
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
launchTakePhoto();
}
});
}
/**
* method that finds storage location on devices. Then
* creates a file and launches an actvitiy for result
* to retrieve the data later and store it in that file,
* in that storage space.
*/
private void launchTakePhoto()
{
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
{
try
{
f = createImageFile();
}
catch(IOException e)
{
Log.i("Error","error");
}
}
else
{
f = new File(getCacheDir(), FILENAME);
}
if (f != null)
{
Uri outputFileUri = Uri.fromFile(f);
i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i, PICTURE_ACTIVITY_CODE);
}
}
protected void onActivityResult(int req, int res, Intent data)
{
if ((req == PICTURE_ACTIVITY_CODE)&&(res == RESULT_OK))
{
Log.i("res: ", "PASSED"); ImageView iv = (ImageView) findViewById(R.id.img);
Uri uriImage = Uri.fromFile(f);
iv.setImageURI(uriImage);
}
}
感谢您的帮助。我已经看到了很多不同的可能答案,但到目前为止我没有任何工作。我只是希望能够返回一个完整尺寸的图像并将其放在图像持有人。
答案 0 :(得分:1)
我认为您可能最好查看用于图像捕获的Android API,因为它最近自API 21以来已更改。可能执行此操作的最佳方法是打开相机,允许用户捕获图像并使用在http://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession.CaptureCallback.html中定义的onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result)
函数用于接收图像捕获的结果,然后将图像查看器设置为结果。如果您还尝试保存图像,请尝试使用Android文档中定义的文件处理程序进程:http://developer.android.com/training/camera/photobasics.html
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}