从摄像机中获取图像并在活动中显示

从摄像机中获取图像并在活动中显示

我想写一个模块,在那里点击一个按钮,相机打开,我可以点击和捕捉一个图像。如果我不喜欢这个图像,我可以删除它,再单击一个图像,然后选择图像,它应该返回并在活动中显示该图像。



犯罪嫌疑人X
浏览 571回答 3
3回答

青春有我

捕捉照片+从画廊选择:        a = (ImageButton)findViewById(R.id.imageButton1);         a.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 selectImage();             }         });     }     private File savebitmap(Bitmap bmp) {       String extStorageDirectory = Environment.getExternalStorageDirectory().toString();       OutputStream outStream = null;      // String temp = null;         File file = new File(extStorageDirectory, "temp.png");       if (file.exists()) {        file.delete();        file = new File(extStorageDirectory, "temp.png");       }       try {        outStream = new FileOutputStream(file);        bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);        outStream.flush();        outStream.close();       } catch (Exception e) {        e.printStackTrace();        return null;       }       return file;      }     @Override     public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.main, menu);         return true;     }      private void selectImage() {             final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };             AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);             builder.setTitle("Add Photo!");             builder.setItems(options, new DialogInterface.OnClickListener() {                 @Override                 public void onClick(DialogInterface dialog, int item) {                     if (options[item].equals("Take Photo"))                     {                         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                         File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");                         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));                         //pic = f;                         startActivityForResult(intent, 1);                     }                     else if (options[item].equals("Choose from Gallery"))                     {                         Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                         startActivityForResult(intent, 2);                     }                     else if (options[item].equals("Cancel")) {                         dialog.dismiss();                     }                 }             });             builder.show();         }         @Override         protected void onActivityResult(int requestCode, int resultCode, Intent data) {             super.onActivityResult(requestCode, resultCode, data);             if (resultCode == RESULT_OK) {                 if (requestCode == 1) {                     //h=0;                     File f = new File(Environment.getExternalStorageDirectory().toString());                     for (File temp : f.listFiles()) {                         if (temp.getName().equals("temp.jpg")) {                             f = temp;                             File photo = new File(Environment.getExternalStorageDirectory(), "temp.jpg");                            //pic = photo;                             break;                         }                     }                     try {                         Bitmap bitmap;                         BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();                         bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),                                 bitmapOptions);                          a.setImageBitmap(bitmap);                         String path = android.os.Environment                                 .getExternalStorageDirectory()                                 + File.separator                                + "Phoenix" + File.separator + "default";                         //p = path;                         f.delete();                         OutputStream outFile = null;                         File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");                         try {                             outFile = new FileOutputStream(file);                             bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);     //pic=file;                             outFile.flush();                             outFile.close();                         } catch (FileNotFoundException e) {                             e.printStackTrace();                         } catch (IOException e) {                             e.printStackTrace();                         } catch (Exception e) {                             e.printStackTrace();                         }                     } catch (Exception e) {                         e.printStackTrace();                     }                 } else if (requestCode == 2) {                     Uri selectedImage = data.getData();                    // h=1;     //imgui = selectedImage;                     String[] filePath = { MediaStore.Images.Media.DATA };                     Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);                     c.moveToFirst();                     int columnIndex = c.getColumnIndex(filePath[0]);                     String picturePath = c.getString(columnIndex);                     c.close();                     Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));                     Log.w("path of image from gallery......******************.........", picturePath+"");                     a.setImageBitmap(thumbnail);                 }             }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android
Java