简体   繁体   English

我想打开Android相机而不将图片保存到图库

[英]I want to open the android camera without saving the picture to gallery

截图

I want the picture to go straight to the ImageView without saving it to gallery if possible. 如果可能的话,我希望图片直接进入ImageView而不将其保存到图库。 As shown in the screenshot, it will ask to save everytime and will save straight to the gallery. 如截图所示,它会要求每次保存并直接保存到图库。 Can this be achieved, or will I have to make my own ImageView camera? 这可以实现,还是我必须制作自己的ImageView相机?

public class Main extends Activity {

ImageView ivPhoto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ivPhoto = (ImageView) findViewById(R.id.ivPic);
}

public void TakePhoto(View v){
    Intent camIntent = new      Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(camIntent,0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==0){
        Bitmap camImage = (Bitmap) data.getExtras().get("data");
        ivPhoto.setImageBitmap(camImage);
    }
}

Finally got what I wanted. 终于得到了我想要的东西。 Thanks guys 多谢你们

public class Main extends Activity {

ImageView ivPhoto;
File myFilesDir;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ivPhoto = (ImageView) findViewById(R.id.ivPic);
    myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.example.project/files");
    System.out.println (myFilesDir);
    myFilesDir.mkdirs();
}

public void TakePhoto(View v){
    Intent camIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    camIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(myFilesDir.toString()+"/temp.jpg")));
    startActivityForResult(camIntent, 0);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==0){
        try {
            Bitmap cameraBitmap;
            cameraBitmap = BitmapFactory.decodeFile(myFilesDir + "/temp.jpg");
            Bitmap.createBitmap(cameraBitmap);
            ivPhoto.setImageBitmap(cameraBitmap);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}


}

Use my Code. 使用我的代码。 I am taking a picture using camera intent and before saving it to gallery , it is showed to the user with a Save and Cancel Button :- Call Camera Intent :- 我正在使用相机意图拍摄照片,然后将其保存到图库,使用“保存并取消”按钮向用户显示: - 调用相机意图: -

                    String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator +CommonFunction.getDateTime()+".jpg"; // Get File Path
                    Intent takePictureFromCameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    takePictureFromCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(SD_CARD_TEMP_DIR)));
                    startActivityForResult(takePictureFromCameraIntent, 123);

onActivityResult : - onActivityResult: -

public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_RESULT) 
        {
            if (resultCode == Activity.RESULT_OK) 
            {
                String galleryImatePath = SD_CARD_TEMP_DIR; // make SD_CARD_TEMP_DIR Global so that you can access it here from camera intent or pass it in put Extra method and retrieve it here

                File f = new File(galleryImatePath);

                try {
                            Bitmap cameraBitmap = null;
                            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                            bmOptions.inJustDecodeBounds = false;
                            bmOptions.inPurgeable = true;
                            bmOptions.inBitmap = cameraBitmap; 
                            bmOptions.inMutable = true; 


                            cameraBitmap = BitmapFactory.decodeFile(galleryImatePath,bmOptions);
                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                            cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);

                            //To Rotate image Code
                                ExifInterface exif = new ExifInterface(galleryImatePath);
                                float rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  
                                System.out.println(rotation);

                            float rotationInDegrees = exifToDegrees(rotation);
                            System.out.println(rotationInDegrees);

                            Matrix matrix = new Matrix();
                            matrix.postRotate(rotationInDegrees);

                            final Bitmap rotatedBitmap = Bitmap.createBitmap(cameraBitmap , 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true);
                            FileOutputStream fos=new FileOutputStream(galleryImatePath);
                            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
                            fos.write(bos.toByteArray());
                            cameraBitmap.recycle();
                            System.gc();
                            fos.flush();
                            fos.close();


                            // To set image in imageview in dialog
                        Capdialog = new Dialog(AddToDo.this,android.R.style.Theme_NoTitleBar_Fullscreen);
                        Capdialog.setContentView(R.layout.captiondialog);
                        Capdialog.setCancelable(false);
                        TextView cancel = (TextView) Capdialog
                                .findViewById(R.id.cancel);
                        TextView done = (TextView) Capdialog.findViewById(R.id.done);
                                                    Capdialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                        ImageView img = (ImageView) Capdialog.findViewById(R.id.image);
                        img.setImageBitmap(rotatedBitmap);
                   }
                   catch(Exception e){}
          }
     }
}

implement your done and cancel on click listener - what you want to do in them. 实现你的完成并取消点击监听器 - 你想要做什么。 My code will capture your image, rotate it in the right direction irrespective of camera rotation and show it to you in a dialog before saving it 我的代码将捕获您的图像,无论相机旋转如何都向右旋转图像并在保存之前在对话框中显示

From my understanding you don't want this to be showing up by any media scanner, like the gallery application. 根据我的理解,您不希望任何媒体扫描仪(如图库应用程序)显示此内容。 What you should actually do is not store it in a root directory like pictures or sdcard, but store it in your applications data folder in the sdcard in Android/data/package/ . 您实际应该做的不是将其存储在图片或SD卡等根目录中,而是将其存储在Android/data/package/的SD卡中的应用程序数据文件夹中。

You can get this using: http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String) 你可以使用: http//developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

File myFilesDir = getExternalFilesDir(null);

OR 要么

File myFilesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

Note that it will only work on API versions 8 or above. 请注意,它仅适用于API版本8或更高版本。

If you don't want to use the function you can simply just use: 如果您不想使用该功能,您只需使用:

File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + packageName + "/files");
myFilesDir.mkdirs();

谷歌提供了一个关于这个确切主题的教程: 控制相机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM