簡體   English   中英

我想打開Android相機而不將圖片保存到圖庫

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

截圖

如果可能的話,我希望圖片直接進入ImageView而不將其保存到圖庫。 如截圖所示,它會要求每次保存並直接保存到圖庫。 這可以實現,還是我必須制作自己的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);
    }
}

終於得到了我想要的東西。 多謝你們

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();
        }
    }
}


}

使用我的代碼。 我正在使用相機意圖拍攝照片,然后將其保存到圖庫,使用“保存並取消”按鈕向用戶顯示: - 調用相機意圖: -

                    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: -

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){}
          }
     }
}

實現你的完成並取消點擊監聽器 - 你想要做什么。 我的代碼將捕獲您的圖像,無論相機旋轉如何都向右旋轉圖像並在保存之前在對話框中顯示

根據我的理解,您不希望任何媒體掃描儀(如圖庫應用程序)顯示此內容。 您實際應該做的不是將其存儲在圖片或SD卡等根目錄中,而是將其存儲在Android/data/package/的SD卡中的應用程序數據文件夾中。

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

File myFilesDir = getExternalFilesDir(null);

要么

File myFilesDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

請注意,它僅適用於API版本8或更高版本。

如果您不想使用該功能,您只需使用:

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