对于我的应用程序,我一直在使用我自己的Camera类来拍摄图像和我自己的数据库但很快就无法跟上变化,我决定使用Android内置的相机应用程序来完成这项工作,但是我似乎无法得到它来保存文件.我在这里错过了什么?该应用程序似乎保存文件,但它只是0字节.我查找了Camera应用程序的源代码,并在Extras中查找"输出"以保存文件.任何帮助将不胜感激.
Public class CameraTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button cameraButton = (Button) findViewById(R.id.cameraButton); cameraButton.setOnClickListener( new OnClickListener(){ public void onClick(View v ){ ContentValues values = new ContentValues(); values.put(Images.Media.TITLE, "title"); values.put(Images.Media.BUCKET_ID, "test"); values.put(Images.Media.DESCRIPTION, "test Image taken"); Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra("output", uri.getPath()); startActivityForResult(intent,0); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode== 0 && resultCode == Activity.RESULT_OK){ ((ImageView)findViewById(R.id.pictureView)).setImageURI(data.getData()); } } }
prasanna.. 16
这与下面的代码一起使用,因为我对最后一个代码感到有些愚蠢.我仍然认为必须有一个更好的方法,以便原始图像仍然保存在某个地方.它仍然向我发送较小的25%大小的图像.
public class CameraTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button cameraButton = (Button) findViewById(R.id.cameraButton); cameraButton.setOnClickListener( new OnClickListener(){ public void onClick(View v ){ Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(intent,0); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode== 0 && resultCode == Activity.RESULT_OK) { Bitmap x = (Bitmap) data.getExtras().get("data"); ((ImageView)findViewById(R.id.pictureView)).setImageBitmap(x); ContentValues values = new ContentValues(); values.put(Images.Media.TITLE, "title"); values.put(Images.Media.BUCKET_ID, "test"); values.put(Images.Media.DESCRIPTION, "test Image taken"); values.put(Images.Media.MIME_TYPE, "image/jpeg"); Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); OutputStream outstream; try { outstream = getContentResolver().openOutputStream(uri); x.compress(Bitmap.CompressFormat.JPEG, 70, outstream); outstream.close(); } catch (FileNotFoundException e) { // } catch (IOException e) { // } } }
此外,我知道Android的杯形蛋糕版本应该很快修复小图像尺寸.
这与下面的代码一起使用,因为我对最后一个代码感到有些愚蠢.我仍然认为必须有一个更好的方法,以便原始图像仍然保存在某个地方.它仍然向我发送较小的25%大小的图像.
public class CameraTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button cameraButton = (Button) findViewById(R.id.cameraButton); cameraButton.setOnClickListener( new OnClickListener(){ public void onClick(View v ){ Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(intent,0); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode== 0 && resultCode == Activity.RESULT_OK) { Bitmap x = (Bitmap) data.getExtras().get("data"); ((ImageView)findViewById(R.id.pictureView)).setImageBitmap(x); ContentValues values = new ContentValues(); values.put(Images.Media.TITLE, "title"); values.put(Images.Media.BUCKET_ID, "test"); values.put(Images.Media.DESCRIPTION, "test Image taken"); values.put(Images.Media.MIME_TYPE, "image/jpeg"); Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); OutputStream outstream; try { outstream = getContentResolver().openOutputStream(uri); x.compress(Bitmap.CompressFormat.JPEG, 70, outstream); outstream.close(); } catch (FileNotFoundException e) { // } catch (IOException e) { // } } }
此外,我知道Android的杯形蛋糕版本应该很快修复小图像尺寸.
以下代码将启动默认摄像头并让摄像头将图像保存到指定的uri.关键是将额外的"MediaStore.EXTRA_OUTPUT"与所需的uri放在一起.
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + image_name + ".jpg"); Uri imageUri = Uri.fromFile(file); Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, 0);