通过默认的相机活动拍摄高质量的图像并将其保存到SD卡中 [英] Taking a high quality image through default camera activity and saving it o the sd card
问题描述
我正在通过默认的相机活动(使用intent.put Extras
)拍摄高分辨率照片,并将其保存到sd卡中,
I am taking a high resolution picture through the default camera activity(using intent.put Extras
),and saving it to the sd card,
代码:
public class CameraActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button takepicture ;
ImageView iv ;
TextView tv;
Button show;
String filepath;
Intent i;
Uri mUri;
final static int cameraData = 0;
File folder = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
takepicture = (Button) findViewById(R.id.button1);
iv = (ImageView) findViewById(R.id.imageView1);
tv = (TextView) findViewById(R.id.textView1);
show = (Button) findViewById(R.id.button2);
takepicture.setOnClickListener(this);
show.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
String sdcardstate = android.os.Environment.getExternalStorageState();
if(sdcardstate.contentEquals(android.os.Environment.MEDIA_MOUNTED)){
filepath = Environment.getExternalStorageDirectory().getPath();
folder = new File(filepath,"wax");
if(!folder.exists()){
try {
folder.createNewFile();
Log.d("folder created", "ya");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mUri = Uri.fromFile(folder);
Log.d("bk", mUri.toString());
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
Log.d("extra", "extra");
startActivityForResult(i,cameraData);
}
break;
case R.id.button2:
File f = new File(filepath,"bmp.png");
Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
iv.setImageBitmap(myBitmap);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
tv.setText("Result ok");
Log.d("ok", "ok");
Bundle extras = data.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
}
}
}
相机活动开始,图像被拍摄,但是当我单击保存"时,它不会返回并且强制关闭.
The camera activity starts , the image is taken , But when i click save , it does not return and force closes.
我已经阅读了很多相关主题,了解到必须在开始摄像头活动之前创建文件,但是仍然不需要.
I have read quite a few threads on this , Learnt that file must be created before the camera activity is started , but still it does not.
请帮助,我在这个问题上停留了一周左右.
Please help , I'm stuck on this problem for a week or so.
Logcat错误
06-15 16:05:50.205: W/dalvikvm(1780): threadid=10: thread exiting with uncaught exception (group=0x4001d800)
06-15 16:05:50.205: E/AndroidRuntime(1780): FATAL EXCEPTION: GLThread 12
06-15 16:05:50.205: E/AndroidRuntime(1780): java.lang.IllegalArgumentException: No configs match configSpec
06-15 16:05:50.205: E/AndroidRuntime(1780): at android.opengl.GLSurfaceView$BaseConfigChooser.chooseConfig(GLSurfaceView.java:760)
06-15 16:05:50.205: E/AndroidRuntime(1780): at android.opengl.GLSurfaceView$EglHelper.start(GLSurfaceView.java:916)
06-15 16:05:50.205: E/AndroidRuntime(1780): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1246)
06-15 16:05:50.205: E/AndroidRuntime(1780): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)
06-15 16:05:50.294: W/ActivityManager(59): Force finishing activity com.android.camera/.Camera
06-15 16:05:50.444: V/camera(1780): stopPreview
推荐答案
使用以下代码实现此目的.
Use following to achieve this.
在调用CameraIntent之前,请根据该文件路径创建一个文件和uri,如下所示.
Before you call CameraIntent create a file and uri based on that filepath as shown here.
filename = Environment.getExternalStorageDirectory().getPath() + "/test/testfile.jpg";
imageUri = Uri.fromFile(new File(filename));
// start default camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
imageUri);
startActivityForResult (cameraIntent, CAMERA_PIC_REQUEST);
现在,您具有可以在onAcityvityResult方法中使用它的文件路径,如下所示,
Now, you have the filepath you can use it in onAcityvityResult method as following,
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode != CAMERA_PIC_REQUEST || filename == null)
return;
ImageView img = (ImageView) findViewById(R.id.image);
img.setImageURI(imageUri);
}
这篇关于通过默认的相机活动拍摄高质量的图像并将其保存到SD卡中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!