如何首先打开图库而不是打开活动视图?

时间:2016-05-22 19:32:36

标签: android

所以我试图找到一种方法来编写代码,当我打开应用程序时,它首先自动打开库,然后当你选择它出现在主要活动上的图片时。怎么做?我需要另一项活动吗?因为现在首先打开主要活动视图,然后您可以单击按钮并选择您想要做的事情。

加载和显示图片的代码:

 ocamera.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);

        }
    });

    if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {


    } else if (shouldShowRequestPermissionRationale(
            Manifest.permission.READ_EXTERNAL_STORAGE)) {
        Toast.makeText(this, "Permission is important to be able edit photos.",
                Toast.LENGTH_SHORT).show();
    }

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

    // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
    // app-defined int constant

    return;


}


@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView im = (ImageView) findViewById(R.id.myimage);
        im.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        BitmapDrawable abmp = (BitmapDrawable) im.getDrawable();
        bmp = abmp.getBitmap();
        if (picturePath != null && bmp != null) {
            int height = bmp.getHeight(), width = bmp.getWidth();

            if (height > 1280 && width > 960) {

                Bitmap bmp = BitmapFactory.decodeFile(picturePath);
                im.setImageBitmap(bmp);
                im.setVisibility(View.VISIBLE);

            } else {

                im.setImageBitmap(bmp);
                im.setVisibility(View.VISIBLE);

            }

        }


    }


}

1 个答案:

答案 0 :(得分:0)

如果您希望您的图库代码首先运行,您只需在活动的onCreate中调用该方法

@Override
public void onCreate(Bundle saveInstanceState){
    super.onCreate(saveInstanceState);

    //you can check permissions here

    //call the method here with code you use to open gallery
    pickImageFromGallery();
}

private pickImageFromGallery(){
   //your code to open gallery here
   Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

   startActivityForResult(i, RESULT_LOAD_IMAGE);
}

这将确保您的图库每次都会打开。您可以选择在onResume()内调用该方法,因为这样可以确保代码运行。

祝你好运,我希望您觉得它很有帮助。