2

截屏

如果可能的话,我希望图片直接进入 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);
    }
}
4

4 回答 4

6

终于得到了我想要的。多谢你们

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


}
于 2013-11-07T09:07:59.793 回答
2

使用我的代码。我正在使用相机意图拍照,在将其保存到图库之前,它会通过保存和取消按钮向用户显示:- 调用相机意图:-

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

活动结果:-

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

在点击侦听器上实现您的完成并取消 - 您想要在其中执行的操作。我的代码将捕获您的图像,无论相机旋转如何,都将其旋转到正确的方向,并在保存之前在对话框中显示给您

于 2013-11-07T04:51:45.673 回答
2

据我了解,您不希望任何媒体扫描仪(例如图库应用程序)显示此内容。您实际上应该做的不是将其存储在图片或 sdcard 之类的根目录中,而是将其存储在 .sdcard 中的应用程序数据文件夹中Android/data/package/

您可以使用: 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();
于 2013-11-07T05:18:40.040 回答
1

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

于 2013-11-07T04:30:25.893 回答