我有點新的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所以,你使用圖像選擇器庫...? –