我们可以在服务等级中打开图片库吗? (并获取图像数据)

时间:2019-04-08 17:01:05

标签: android android-intent image-gallery android-gallery

我的问题可能很简单,但是我是android studio的新手,所以一切对我来说都有些困难。 我的问题是,我正在开发的应用程序需要从Service Class进入Image Gallery以获取图像并在另一个函数中使用它。从关于请求权限的第一点开始。由于该权限无法在Service中使用,因此我尝试将其放在主要活动上以进行首次权限,然后该服务可以再次使用它而无需获得权限。 (我想知道是否还有其他方法可以解决此问题,但是现在还可以。)接下来的事情是,在Service中,我不能使用startActivityForResult()和onActivityResult()来选择图像并获取结果。

对于我的代码,我已经完成了权限部分。我的应用程序是一个浮动窗口小部件,因此我必须在此处放置2个权限。

公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {

private static final int DRAW_OVER_OTHER_APP_PERMISSION_REQUEST_CODE = 123;
private static final int PHOTO_LIBRARY_REQUEST_CODE_IMAGEVIEW = 122;
private static final int REQUEST_PERMISSION = 12;
private TextView textHelp;
private RelativeLayout rlOverlay;
private Button getPermission;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textHelp = findViewById(R.id.textHelp);
    textHelp.setOnClickListener(this);
    rlOverlay = findViewById(R.id.rlOverlay);
    getPermission = findViewById(R.id.getImagePermission);
    getPermission.setOnClickListener(this);

}

public void onClick(View view) {
    switch (view.getId()) {

        case R.id.textHelp:
            if (textHelp.getText().toString().equals("Next")) {
                textHelp.setText("Got It");
            } else {
                rlOverlay.setVisibility(View.GONE);
            }
            break;
        case R.id.getImagePermission:
            attemptToOpenGallery();
        default:
            break;
    }
}

/*  start floating widget service  */
public void createFloatingWidget(View view) {
    //Check if the application has draw over other apps permission or not?

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {

        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, DRAW_OVER_OTHER_APP_PERMISSION_REQUEST_CODE);
    } else

        startFloatingWidgetService();

}

public void attemptToOpenGallery() {

    int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;


    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {


        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) {


        } else {

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

            return;
        }
    } else {


        System.out.println("user already authorized external storage access. Continue");
        openImageGallery();
    }
}

public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_PERMISSION) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // Permission granted. Continue with image selection

            openImageGallery();

        } else {
            // User refused to grant permission. handle this issue however you wish
        }
    }
}



public void openImageGallery() {


    startActivityForResult(new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI), PHOTO_LIBRARY_REQUEST_CODE_IMAGEVIEW);

}

/*  Start Floating widget service and finish current activity */
private void startFloatingWidgetService() {
    startService(new Intent(MainActivity.this, FloatingWidgetService.class));
    finish();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == DRAW_OVER_OTHER_APP_PERMISSION_REQUEST_CODE) {
        //Check if the permission is granted or not.
        if (resultCode == RESULT_OK)
            //If permission granted start floating widget service
            startFloatingWidgetService();
        else
            //Permission is not available then display toast
            Toast.makeText(this,
                    getResources().getString(R.string.draw_other_app_permission_denied),
                    Toast.LENGTH_SHORT).show();

    } else if (requestCode == PHOTO_LIBRARY_REQUEST_CODE_IMAGEVIEW && resultCode == RESULT_OK && data != null){

        Uri mediaUri = data.getData();

        System.out.println("Selected media uri: " + mediaUri);


    }else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

}

对于服务类,我尝试将openGallery函数放入onFloatingWidgetClick()

公共类FloatingWidgetService扩展了Service实现的View.OnClickListener {

private void onFloatingWidgetClick() {
    if (isViewCollapsed()) {

        // create open gallery without concerning permission
        startActivityForResult(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI), PHOTO_LIBRARY_REQUEST_CODE_IMAGEVIEW);

        // then get uri from gallery and change it to bitmap

    }
}

}

所以我想要的结果是能够打开Gallery并选择图片并在Service类中使用它。有办法吗?

0 个答案:

没有答案