我有点新的Android应用程序开发,并努力做一个简单的应用程序,它,的Android捕捉和显示图像的应用程序
- 使用系统相机应用程序通过将摄像机的意图进行拍照。
- 将图像存储在文件系统中的公用文件夹中。
- 然后将图像从其绝对路径转换为位图,并通过在图像视图中执行一些缩放来显示它。
这里是我的代码....
static final int REQUEST_IMAGE_CAPTURE = 1;
String mCurrentPhotoPath;
Button captureImageButton;
static ImageView imageCapturedView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_image);
captureImageButton = (Button) findViewById(R.id.button);
OnClickListener captureImageListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent captureImageIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(captureImageIntent.resolveActivity(getPackageManager())!=null)
{
File photoFile=null;
try{
photoFile = createImageFile();
}catch(IOException e){};
if(photoFile != null)
{
captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(captureImageIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
};
captureImageButton.setOnClickListener(captureImageListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
imageCapturedView = (ImageView)findViewById(R.id.ImageView);
if(requestCode==REQUEST_IMAGE_CAPTURE && resultCode==RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap imageBitmap= (Bitmap) extras.get("data");
imageCapturedView.setImageBitmap(imageBitmap);
galleryAddPic();
setPic();
}
}
private File createImageFile() throws IOException
{
String TimeStamp = new SimpleDateFormat("yyyyMMDdd_HHmmss").format(new Date());
String ImageFile = "JPEG_" + TimeStamp + "_";
File StorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(ImageFile, ".jpg", StorageDir);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void galleryAddPic()
{
Intent mediaScan = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f= new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScan.setData(contentUri);
this.sendBroadcast(mediaScan);
}
private void setPic() throws ArithmeticException{
int scaleFactor;
Bitmap bitmap;
// Get the dimensions of the View
int targetW = imageCapturedView.getWidth();
int targetH = imageCapturedView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
//bmOptions.inSampleSize = 4;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
scaleFactor= Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
Toast.makeText(getApplicationContext(), mCurrentPhotoPath, Toast.LENGTH_LONG).show();
imageCapturedView.setImageBitmap(bitmap);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.take_image, menu);
return true;
}
}
这里的问题在于,根据我的setPic功能,但不知道它是什么,因为我刚才跟着教程给开发者.android
我能够捕捉图像并将其存储在SD卡中,但无法显示它在图像视图。根据图像视图属性缩放位图时发生错误。
这是我收到
01-20 14:32:34.736: E/AndroidRuntime(2513): FATAL EXCEPTION: main
01-20 14:32:34.736: E/AndroidRuntime(2513): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/Pictures/JPEG_2014012020_143223_-1472164486.jpg typ=image/jpeg (has extras) }} to activity {yogesh.atArxxus.ocr_trial_application/yogesh.atArxxus.ocr_trial_application.Take_image}: java.lang.ArithmeticException: divide by zero
01-20 14:32:34.736: E/AndroidRuntime(2513): at android.app.ActivityThread.deliverResults(ActivityThread.java:3161)
01-20 14:32:34.736: E/AndroidRuntime(2513): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3204)
01-20 14:32:34.736: E/AndroidRuntime(2513): at android.app.ActivityThread.access$1100(ActivityThread.java:137)
01-20 14:32:34.736: E/AndroidRuntime(2513): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1254)
01-20 14:32:34.736: E/AndroidRuntime(2513): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 14:32:34.736: E/AndroidRuntime(2513): at android.os.Looper.loop(Looper.java:213)
01-20 14:32:34.736: E/AndroidRuntime(2513): at android.app.ActivityThread.main(ActivityThread.java:4793)
01-20 14:32:34.736: E/AndroidRuntime(2513): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 14:32:34.736: E/AndroidRuntime(2513): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 14:32:34.736: E/AndroidRuntime(2513): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
01-20 14:32:34.736: E/AndroidRuntime(2513): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
01-20 14:32:34.736: E/AndroidRuntime(2513): at dalvik.system.NativeStart.main(Native Method)
01-20 14:32:34.736: E/AndroidRuntime(2513): Caused by: java.lang.ArithmeticException: divide by zero
01-20 14:32:34.736: E/AndroidRuntime(2513): at yogesh.atArxxus.ocr_trial_application.Take_image.setPic(Take_image.java:136)
01-20 14:32:34.736: E/AndroidRuntime(2513): at yogesh.atArxxus.ocr_trial_application.Take_image.onActivityResult(Take_image.java:87)
01-20 14:32:34.736: E/AndroidRuntime(2513): at android.app.Activity.dispatchActivityResult(Activity.java:5192)
01-20 14:32:34.736: E/AndroidRuntime(2513): at android.app.ActivityThread.deliverResults(ActivityThread.java:3157)
谢谢
如果我删除缩放代码,只是试图显示在图像视图图像那么它正在工作。不知道什么是错的一些我如何获得targetW targetH这个值在某个地方为零。 –
我也有类似的问题。所以请检查下面的链接,它可能对你有用。 http://stackoverflow.com/questions/21136311/picking-a-photo-from-gallery-and-show-in-a-image-view – Biplab
@Biplab所以,你使用图像选择器库...? –