如何放置相机图像而不是可绘制图像?

时间:2017-07-15 14:28:21

标签: android bitmap android-camera android-imageview drawable

我只是编程新手。我只想在第二个活动中放置我的捕获图像而不是可绘制的图像。我需要帮助下面的imagePath ..代码

MainActivity

    Button btn_cam = (Button) findViewById(R.id.btn_cam);
    btn_cam.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            file = new File(MainActivity.this.getExternalCacheDir(),
                    String.valueOf(System.currentTimeMillis()) + ".jpg");
            fileUri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            MainActivity.this.startActivityForResult(intent, CAMERA_PIC_REQUEST);

        }
    });                                                                     }                                                                       
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) { 
            fileUri = data.getData();
            Intent cam = new Intent(this, MainCam.class);
            cam.putExtra("flostic", fileUri.toString());
            startActivity(cam);
            this.finish();
        }

第二项活动

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(image_path, options);
            view.setImageBitmap(bitmap);

        }

2 个答案:

答案 0 :(得分:0)

根据您的评论,您希望在第二个活动中获取图像路径。这是关键的一点。

String image_path = getIntent().getStringExtra("flostic");

以下是完整代码。

第一项活动:

Button btnCam = (Button) findViewById(R.id.btn_cam);
btnCam.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        File file = new File(Environment.getExternalStorageDirectory(),
                String.valueOf(System.currentTimeMillis()) + ".jpg");
        Uri fileUri = Uri.fromFile(file);

        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(i, CAMERA_PIC_REQUEST);
    }
});                                                                             

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

    if(requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) { 
        Intent cam = new Intent(this, MainCam.class);
        cam.putExtra("flostic", data.getDate().toString());
        startActivity(cam);
        finish();
    }
}

第二项活动:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout):

    ImageView view = (ImageView) findView(R.id.your_image_view);

    Intent i = getIntent();
    String imagePath = i.getStringExtra("flostic");

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

    view.setImageBitmap(bitmap);
}

答案 1 :(得分:0)

首先在清单

中添加这些权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

如果您的Android版本大于lolipop,则向您的应用添加运行时权限。 See Documentation here

MainActivity

将onClickListener()放到相机按钮

 btn_cam.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 1);
        }
    });

然后在onActivityResult()中获取所选图像URI的图像路径

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

    Uri uri=data.getData();
    switch (requestCode){


        case 1:
            if (resultCode==RESULT_OK){

                String path=getRealPathFromURI(uri);

                Toast.makeText(this, ""+path, Toast.LENGTH_SHORT).show();

                Intent intent=new Intent(this,SecondActivity.class);
                intent.putExtra("imagepath",path);
                startActivity(intent);
            }
    }
}

包含返回通过相机拍摄的图像路径的getRealPathFromURI()方法。使用intent.putExtra()将图像路径发送到第二个活动。

   public String getRealPathFromURI(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

此代码适用于SecondActivity

然后在第二个活动中从第一个活动获取数据(图像路径)并将其存储在字符串变量中并从该图像路径创建位图并将位图图像设置为您的imageview

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);

    String imagepath=getIntent().getExtras().getString("imagepath");


    File image_path=new File(imagepath);
    if (image_path.exists()){

        Bitmap imageBitmap = BitmapFactory.decodeFile(image_path.getAbsolutePath());

        ImageView imageView = (ImageView) findViewById(R.id.imageselect);

        imageView.setImageBitmap(imageBitmap);
    }

} }

这是您在第一项活动中采取的第二项活动中显示摄像机图像的方法