分享从相机拍摄的图像

时间:2017-10-07 15:43:15

标签: java android android-intent android-camera-intent

我正在开发一款应用程序,可以用相机拍照,并允许用户分享。但是股权意图并没有奏效。我已经检查过,但没有人在我的设备上工作。我在Android 6.0 marshmallow平板电脑上运行该应用程序。

这是我的代码

public class SharePic extends Activity {

static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView picView;
Button shareBtn;
Bitmap imageBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_share_pic);
    picView = (ImageView) findViewById(R.id.picView);
    shareBtn = (Button) findViewById(R.id.shareBtn);

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);


    }
    }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        imageBitmap = (Bitmap) extras.get("data");
        picView.setImageBitmap(imageBitmap);
    }
}


public void share(View view){
    String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap,"title", null);
    Uri bitmapUri = Uri.parse(bitmapPath);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/png");
    intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
    startActivity(Intent.createChooser(intent , "Share"));
}
}

任何帮助?

1 个答案:

答案 0 :(得分:0)

因此,何时调用共享按钮。 我猜你可能必须在分享按钮上设置事件监听器setOnClickListener,以防你没有在XML中设置它

shareBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                share(v);
            }
        });