从相机捕获照片并在另一个活动的图像视图中显示它

时间:2016-09-18 18:50:11

标签: android bitmap android-bitmap

我想在图像视图中的另一个活动中显示摄像头拍摄的照片。但它必须是完整的质量。请告诉我该怎么做。这是我的代码:

MainActivity.class

 private static final int REQUEST_CAMERA = 1888;
 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,REQUEST_CAMERA);
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
     if (requestCode == REQUEST_CAMERA)
            {
               Uri returnUri = data.getData();
                try {
                    Bitmap bitmapImage = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(),returnUri);
                    Intent i = new Intent(this,ShowCameraPhotoActivity.class);
                    i.putExtra("image",bitmapImage);
                    startActivity(i);
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
           }

                }

ShowPhotoCameraActivity.class:

  ImageView showPhoto = (Imageview) findViewById(R.id.image);
   Bitmap bitmap = (Bitmap) this.getIntent().getParcelableExtra("image");
   if (showPhoto != null) {
        showPhoto.setImageBitmap(bitmap);
    }

4 个答案:

答案 0 :(得分:0)

Utils Class。

 public static File getOutputMediaFile(int type) {
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            Config.IMAGE_DIRECTORY_NAME);

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(Config.IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + Config.IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == Config.MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    }  else {
        return null;
    }

    return mediaFile;
}


public static  File persistImage(Bitmap bitmap, String name) {
    File filesDir = AppSingleton.getContext().getFilesDir();
    File imageFile = new File(filesDir, name + ".png");

    OutputStream os;
    try {
        os = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
        os.flush();
        os.close();
    } catch (Exception e) {
        Log.e(AppSingleton.getContext().getClass().getSimpleName(), "Error writing bitmap", e);
    }
    return imageFile;
}



public static Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

MainActivity

    private static final int PICK_IMAGE_REQUEST = 2;
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;

    private Uri fileUri;
    Bitmap bitmap2;


 /**
 * Cature image
 */
private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = Utils.getOutputMediaFileUri(Config.MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            String imagePath = getImagePath(data.getData());
            if (imagePath != null) {
                Intent intent = new Intent(this, PreviewActivity.class);
                intent.putExtra(IMAGE_PATH, imagePath);
                startActivity(intent);
            }
        }
    }
}

SecondActivity

onCreate()

中的

BitmapFactory.Options options = new BitmapFactory.Options();
            options.inScaled=true;
Bitmap bitmap = BitmapFactory.decodeFile(getIntent().getStringExtra("IMAGE_PATH"), options);
imageview.setImageBitmap(bitmap);

答案 1 :(得分:0)

你可以这样做。打开相机。

Intent intent = getIntent();
Uri uri = (Uri) intent.getExtras().get(Intent.EXTRA_STREAM);
imageView.setImageURI(imgUri);
拍照后,您可以像这样收集onActivityResult()方法中的数据。

{{1}}

然后新活动就是这样。

{{1}}

答案 2 :(得分:0)

你的答案没有对齐,导致崩溃。

全局变量:private static final int REQUEST_CAMERA = 1888;

调用相机的功能:

      private void camera(){
      Intent intent = (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
                    ? new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE)
                    : new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      startActivityForResult(intent,REQUEST_CAMERA);
      }

结果处理的另一个@Override方法onActivityResult

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (resultCode == RESULT_OK) {
      if (requestCode == REQUEST_CAMERA)
        {
            try {
                Bitmap bitmapImage = (Bitmap)data.getExtra("data");

                Intent i = new Intent(this,ShowCameraPhotoActivity.class);
                i.putExtra("image",bitmapImage);
                startActivity(i);
                } catch (IOException e) {
                e.printStackTrace();
                }
        }
       }
      }

必须在Camera

中声明Manifest.xml的权限
<uses-permission android:name="android.permission.CAMERA"/>

答案 3 :(得分:0)

您可以使用Firebase存储图像,并使用图像url在您选择的任何活动中加载图像 我在我的应用https://youtu.be/s1aOlr3vbbk中使用了此tut 您可以使用Picasso或glide库加载图像

 private DatabaseReference reference;
 private ImageView image_profile;

image_profile = (ImageView) findViewById(R.id.image_profile);
        reference = FirebaseDatabase.getInstance().getReference();

        Query query = reference.child("Pictures");

        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                String url = snapshot.child("imageurl").getValue().toString();

                if (!url.isEmpty())
                {
                    Picasso.get()
                            .load(url)
                            .into(image_profile);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });